SNA Descritive Analysis from “Projeto Redes de Atenção às pessoas que consomem álcool e outras Drogas em Juiz de Fora-MG Brazil” - SNArRDJF

Here you can find a basic script to analysis data from SNArRDJF - this script was elaborated considering its use for orther matrix adjacency data from SNArRDJF - Here we are going to analyse:

1 fuull_no_zero_fancy

########################## Basic Preparation ##### `#########################

1.1 Loading objects generated with 1.Principal.Rmd Script - Please run this script only after run 1.Principal.Rmd

1.2 Reload packages

suppressMessages(library(RColorBrewer))
suppressMessages(library(car))
suppressMessages(library(xtable))
suppressMessages(library(igraph))
suppressMessages(library(magrittr))
suppressMessages(library(keyplayer))
suppressMessages(library(dplyr))
suppressMessages(library(visNetwork))
suppressMessages(library(knitr))
suppressMessages(library(DT))

1.3 Adding phantom tools

#In order to get dinamic javascript object install those ones. If you get problems installing go to Stackoverflow.com and type your error to discover what to do. In some cases the libraries need to be intalled in outside R libs.
#devtools::install_github("wch/webshot")
#webshot::install_phantomjs()

1.4 Setting a random seed - this is a good strategy to keep the same graph pattern layout in a new report generation

set.seed(123)

2 Removing CAPS AD

full_no_zero_without_CAPS<-delete_vertices(full_no_zero,V(full_no_zero)[V2_LABEL_ID=="q170_CAPS...CAPS.AD"])

2.1 Vertices and Edges Number

#1. Vertices and Edges
full_no_zero_without_CAPS_vcount<-vcount(full_no_zero_without_CAPS)
full_no_zero_without_CAPS_ecount<-ecount(full_no_zero_without_CAPS)

2.2 Vertice Number

Each vertice represents a service - named as an actor in our network

vcount(full_no_zero_without_CAPS)
## [1] 186

2.3 Edge Number

Each edge represents a connection between two services named as a tie

ecount(full_no_zero_without_CAPS)
## [1] 1369

3 Density - The proportion of present edges from all possible edges in the network.

The density of a binary network is simply the proportion of all possible ties that are actually present.

For a valued network, density is defined as the sum of the ties divided by the number of possible ties (i.e. the ratio of all tie strength that is actually present to the number of possible ties).

The density of a network may give us insights into such phenomena as the speed at which information diffuses among the nodes, and the extent to which actors have high levels of social capital and/or social constraint.

3.1 Edge Density

The density of a graph is the ratio of the number of edges and the number of possible edges.

edge_density_full_no_zero_without_CAPS<-edge_density(full_no_zero_without_CAPS) #The proportion of present edges from all possible edges in the network.
edge_density_full_no_zero_without_CAPS
## [1] 0.03978495

3.2 Edge connectivity - Adhesion

The edge connectivity of a graph or two vertices, this is recently also called group adhesion.

edge_connectivity(full_no_zero_without_CAPS, source =NULL, target =NULL, checks = T) #source and target can be replaced - their are here just as default
## [1] 0

3.2.1 Adhesion example

In order to use this we need to call source and target using the number of each vertex instead of the name - type in R to get numbers

#Names and numbers

# list all if you have no idea about services id
# V(full_no_zero_without_CAPS)$name 

# list all three first (you can use c(1:3))
V(full_no_zero_without_CAPS)$name[1]  # just as an example
## [1] "ASS_HOS_ Hospital de Pronto Socorro – HPS"
V(full_no_zero_without_CAPS)$name[2]  # just as an example
## [1] "AMB_SAM_ Centro de Atenção à Saúde Mental (CASM)"
V(full_no_zero_without_CAPS)$name[3]  # just as an example
## [1] "CRAS_AS_ CRAS Sudeste Costa Carvalho"
# list by others id's
V(full_no_zero_without_CAPS)$name[6]  # just as an example
## [1] "ASS_HOS_ Serviço de Controle e Prevenção e Tratamento do Tabagismo (SECOPTT)"
V(full_no_zero_without_CAPS)$name[150]  # just as an example
## [1] "UAP_RUR_ Jacutinga"
V(full_no_zero_without_CAPS)$name[185]  # just as an example
## [1] "AJU_MUT_ Grupo N.A Rendição"

3.3 Edge Connectivity - “CAPS_AD”==3 and “UAPS RURAL Buiéié”==150

Point connection calculates the number of nodes that would have to be removed in order for one actor to no longer be able to reach another. If there are many different pathways that connect two actors, they have high “connectivity” in the sense that there are multiple ways for a signal to reach from one to the other - lower number - worse resilience

edge_connectivity(full_no_zero_without_CAPS, source =150, target =3, checks = T) 
## [1] 1

4 Vertex Connectivity - Group Cohesion

It is the minimum number of vertices needed to remove to make the graph not strongly connected. (If the graph is not strongly connected then this is zero.). The cohesion of a graph (as defined by White and Harary, see references), is the vertex connectivity of the graph. This is calculated by cohesion.

These three functions essentially calculate the same measure(s), more precisely vertex_connectivity is the most general, the other two are included only for the ease of using more descriptive function names.

vertex_connectivity(full_no_zero_without_CAPS, source = NULL, target = NULL, checks = TRUE)
## [1] 0

4.1 Cohesion example

In order to use this we need to call source and target using the number of each vertex instead of the name - type in R to get numbers - see example above for more details

4.2 Vertex Connectivity - “CRE_SOC_ CREAS Norte”==6 and “AJU_MUT_ Grupo A.A. Liberdade” ==185

Minimum number of vertices needed to remove to make the vertex not connected by any vertex - it leads an error in case of using two connected vertex

vertex_connectivity(full_no_zero_without_CAPS, source =6, target =185, checks = T) 
## [1] 1

5 Centrality Measures

• For undirected graphs:

– Actor centrality - involvement (connections) with other actors

• For directed graphs:

– Actor centrality - source of the ties (outgoing edges)

– Actor prestige - recipient of many ties (incoming edges)

In general - high centrality degree means direct contact with many other actors

5.1 Centrality Degree (number of ties/nearest neighbors).

5.2 Saving in igrpah object

V(full_no_zero_without_CAPS)$full_no_zero_indegree<-degree(full_no_zero_without_CAPS, mode = "in") # Actor prestige - recipient of many ties (incoming edges)
V(full_no_zero_without_CAPS)$full_no_zero_outdegree <- degree(full_no_zero_without_CAPS, mode = "out") # Actor centrality - source of the ties (outgoing edges)
V(full_no_zero_without_CAPS)$full_no_zero_totaldegree <- degree(full_no_zero_without_CAPS, mode = "total")

5.3 Saving in Global Environment as an object

full_no_zero_without_CAPS_indegree<-degree(full_no_zero_without_CAPS, mode = "in")
full_no_zero_without_CAPS_outdegree<-degree(full_no_zero_without_CAPS, mode = "out")
full_no_zero_without_CAPS_totaldegree<-degree(full_no_zero_without_CAPS, mode = "total")

6 Centrality Degree Descriptive Statistics - non-normalized

6.1 Centrality Degree Descriptive Statistics - In

##in
summary(full_no_zero_without_CAPS_indegree)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##    0.00    3.00    5.00    7.36    8.00   69.00
sd(full_no_zero_without_CAPS_indegree)
## [1] 7.954215

6.2 Histogram full_no_zero_without_CAPS degree - In

hist(degree(full_no_zero_without_CAPS, mode = "in", normalized = F), ylab="Frequency", xlab="Degree",  breaks=vcount(full_no_zero_without_CAPS)/10, main="Histogram of Indegree Nodes - fuull_no_zero_fancy")

6.3 Centrality Degree Descriptive Statistics - Out

##out
summary(full_no_zero_without_CAPS_outdegree)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##    0.00    1.00    3.00    7.36    8.00   88.00
sd(full_no_zero_without_CAPS_outdegree)
## [1] 11.89932

6.4 Histogram full_no_zero_without_CAPS degree - Out

hist(degree(full_no_zero_without_CAPS, mode = "out", normalized = F), ylab="Frequency", xlab="Degree",  breaks=vcount(full_no_zero_without_CAPS)/10, main="Histogram of Outdegree Nodes - fuull_no_zero_fancy")

6.5 Centrality Degree Descriptive Statistics - All

##all
summary(full_no_zero_without_CAPS_totaldegree)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##    0.00    4.00    9.00   14.72   17.00  157.00
sd(full_no_zero_without_CAPS_totaldegree)
## [1] 18.31016

6.6 Histogram full_no_zero_without_CAPS degree - All

hist(degree(full_no_zero_without_CAPS, mode = "all", normalized = F), ylab="Frequency", xlab="Degree",  breaks=vcount(full_no_zero_without_CAPS)/10, main="Histogram of All Degree Nodes - fuull_no_zero_fancy")

7 Compute strength - weighted

A slightly more nuanced metric is “strength centrality”, which is defined as the sum of the weights of all the connections for a given node. This is also sometimes called “weighted degree centrality”

V(full_no_zero_without_CAPS)$full_no_zero_strength<- strength(full_no_zero_without_CAPS, weights=E(full_no_zero_without_CAPS)$weight)
full_no_zero_strength<- strength(full_no_zero_without_CAPS, weights=E(full_no_zero_without_CAPS)$weight)

7.1 Strength Stats

summary(full_no_zero_strength)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##    0.00    4.00    9.00   14.72   17.00  157.00
sd(full_no_zero_strength)
## [1] 18.31016

7.2 Histogram full_no_zero_without_CAPS degree - All

hist(strength(full_no_zero_without_CAPS, weights=E(full_no_zero_without_CAPS)$weight), ylab="Frequency", xlab="Degree",  breaks=vcount(full_no_zero_without_CAPS)/10, main="Histogram of Strength Degree Nodes - fuull_no_zero_fancy")

8 Centrality Degree Normalized

8.1 Centrality Degree Normalized saving igraph object

V(full_no_zero_without_CAPS)$full_no_zero_indegree_n<-degree(full_no_zero_without_CAPS, mode = "in", normalized = T)
V(full_no_zero_without_CAPS)$full_no_zero_outdegree_n<- degree(full_no_zero_without_CAPS, mode = "out", normalized = T)
V(full_no_zero_without_CAPS)$full_no_zero_totaldegree_n<- degree(full_no_zero_without_CAPS, mode = "total", normalized = T)

8.2 Saving in Global Environment as an object

full_no_zero_without_CAPS_indegree_n<-degree(full_no_zero_without_CAPS, mode = "in", normalized = T)
full_no_zero_without_CAPS_outdegree_n<-degree(full_no_zero_without_CAPS, mode = "out", normalized = T)
full_no_zero_without_CAPS_totaldegree_n<-degree(full_no_zero_without_CAPS, mode = "total", normalized = T)

8.3 Centrality Degree Normalized Descriptive Statistics - in

summary(full_no_zero_without_CAPS_indegree_n)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
## 0.00000 0.01622 0.02703 0.03978 0.04324 0.37300
sd(full_no_zero_without_CAPS_indegree_n)
## [1] 0.04299576

8.4 Histogram full_no_zero_without_CAPS degree normalized - in

hist(degree(full_no_zero_without_CAPS, mode = "in", normalized = T), ylab="Frequency", xlab="Normalized Degree",  breaks=vcount(full_no_zero_without_CAPS)/10, main="Histogram of Normalized Indegree Nodes - fuull_no_zero_fancy")

8.5 Centrality Degree Normalized Descriptive Statistics - out

summary(full_no_zero_without_CAPS_outdegree_n)
##     Min.  1st Qu.   Median     Mean  3rd Qu.     Max. 
## 0.000000 0.005405 0.016220 0.039780 0.043240 0.475700
sd(full_no_zero_without_CAPS_outdegree_n)
## [1] 0.06432066

8.6 Histogram full_no_zero_without_CAPS degree normalized - out

hist(degree(full_no_zero_without_CAPS, mode = "out", normalized = T), ylab="Frequency", xlab="Normalized Degree",  breaks=vcount(full_no_zero_without_CAPS)/10, main="Histogram of Normalized Outdegree Nodes - fuull_no_zero_fancy")

8.7 Centrality Degree Normalized Descriptive Statistics - all

summary(full_no_zero_without_CAPS_totaldegree_n)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
## 0.00000 0.02162 0.04865 0.07957 0.09189 0.84860
sd(full_no_zero_without_CAPS_totaldegree_n)
## [1] 0.09897384

8.8 Histogram full_no_zero_without_CAPS degree normalized - all

hist(degree(full_no_zero_without_CAPS, mode = "all", normalized = T), ylab="Frequency", xlab="Normalized Degree",  breaks=vcount(full_no_zero_without_CAPS)/10, main="Histogram of Normalized All Degree Nodes - fuull_no_zero_fancy")

9 Centralization Degree

V(full_no_zero_without_CAPS)$full_no_zero_centr_degree <- centralization.degree(full_no_zero_without_CAPS)$res
full_no_zero_centr_degree <- centralization.degree(full_no_zero_without_CAPS)

9.1 Centralization

full_no_zero_centr_degree$centralization
## [1] 0.386618

9.2 Theoretical Max

full_no_zero_centr_degree$theoretical_max
## [1] 68450

10 Degree distribution considering total equal one

full_no_zero_without_CAPS_degree.distribution<-degree.distribution(full_no_zero_without_CAPS)

10.1 Degree distribution Descriptive Stats

summary(full_no_zero_without_CAPS_degree.distribution)
##     Min.  1st Qu.   Median     Mean  3rd Qu.     Max. 
## 0.000000 0.000000 0.000000 0.006329 0.005376 0.112900
sd(full_no_zero_without_CAPS_degree.distribution)
## [1] 0.01785215

10.2 Histogram full_no_zero_without_CAPS distribution degree

hist(degree.distribution(full_no_zero_without_CAPS), breaks=vcount(full_no_zero_without_CAPS)/10, ylab="Frequency", xlab="Degree Distribuition", main="Histogram of Degree Distribuition - fuull_no_zero_fancy")

10.3 Degree Probability Distribution

dd <- degree.distribution(full_no_zero_without_CAPS, cumulative=T, mode="all")

10.4 Degree Probability Distribution - Plot Cumulative Frequency

plot(dd, pch=19, cex=1, col="orange", xlab="Degree", ylab="Cumulative Frequency", main= "Cumulative Frequency of fuull_no_zero_fancy ")

11 Log-Log Degree Distribution - Scale Free Network - Does it fit to power law ?

dd.full_no_zero_without_CAPS <- degree.distribution(full_no_zero_without_CAPS)
d <- 1:max(degree(full_no_zero_without_CAPS))-1
ind <- (dd.full_no_zero_without_CAPS != 0)

11.1 Plot Log-Log Degree Distribution

plot(d[ind], 
     dd.full_no_zero_without_CAPS[ind], 
     log="xy", 
     col="blue",
     xlab=c("Log-Degree"), 
     ylab=c("Log-Intensity"),
     main="Log-Log Degree Distribution For fuull_no_zero_fancy"
     )

12 Average Neighbor Degree versus Vertex Degree (log-log scale for fuull_no_zero_fancy)

The neighborhood of a given order y of a vertex v includes all vertices which are closer to v than the order. Ie. order y=0 is always v itself, order 1 is v plus its immediate neighbors, order 2 is order 1 plus the immediate neighbors of the vertices in order 1, etc.

12.1 Simplify graph first

full_no_zero_without_CAPS_simplified<-simplify(full_no_zero_without_CAPS)

12.2 Average Neighbor Degree versus vertex degree (log-log scale for full_no_zero_without_CAPS)

full_no_zero_without_CAPS_a.nn.deg <- graph.knn(full_no_zero_without_CAPS_simplified, weights =E(full_no_zero_without_CAPS_simplified)$weight)$knn %>% round(1)

12.3 Saving to igraph object

V(full_no_zero_without_CAPS_simplified)$full_no_zero_without_CAPS_a.nn.deg <- graph.knn(full_no_zero_without_CAPS_simplified, weights=E(full_no_zero_without_CAPS_simplified)$weight)$knn

12.4 Table Average Neighbor Degree

d<-cbind(V(full_no_zero_without_CAPS_simplified)$LABEL_COR,full_no_zero_without_CAPS_a.nn.deg)
datatable(d)

12.5 Plotting Average Neighbor Degree versus vertex degree

plot(degree(full_no_zero_without_CAPS_simplified), 
     full_no_zero_without_CAPS_a.nn.deg, 
     log="xy", 
     col="goldenrod", 
     xlab=c("Log Vertex Degree"),
     ylab=c("Log Average Neighbor Degree"),
     main="Average Neighbor Degree vs Vertex Degree - Log-Log Scale for fuull_no_zero_fancy"
     )

13 Average Weighted Neighbor Degree versus vertex degree (log-log scale for weighted fuull_no_zero_fancy)

full_no_zero_without_CAPS_a.nn.deg_w <- graph.knn(full_no_zero_without_CAPS_simplified, weights=E(full_no_zero_without_CAPS_simplified)$weight)$knn %>% round(1)

13.1 Saving to igraph object

V(full_no_zero_without_CAPS_simplified)$full_no_zero_without_CAPS_a.nn.deg_w <-full_no_zero_without_CAPS_a.nn.deg <- graph.knn(full_no_zero_without_CAPS_simplified, weights=E(full_no_zero_without_CAPS_simplified)$weight)$knn

13.2 Average Weighted Neighbor Descriptive

summary(full_no_zero_without_CAPS_a.nn.deg_w)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max.    NA's 
##    4.00   29.20   41.20   46.15   56.90  128.50       1
sd(full_no_zero_without_CAPS_a.nn.deg_w, na.rm = T)
## [1] 25.09652

13.3 Table Average Neighbor Degree Weighted

d<-cbind(V(full_no_zero_without_CAPS_simplified)$LABEL_COR,full_no_zero_without_CAPS_a.nn.deg_w)
datatable(d)

13.4 Plotting Average Neighbor Degree versus vertex degree

plot(degree(full_no_zero_without_CAPS_simplified), 
     full_no_zero_without_CAPS_a.nn.deg, 
     log="xy", 
     col="goldenrod", 
     xlab=c("Log Vertex Degree"),
     ylab=c("Log Average Neighbor Degree"),
     main="Average Weighted Neighbor Degree vs Vertex Degree - Log-Log Scale For Weighted fuull_no_zero_fancy"
     )

14 Degree Centralities Dinamic Table

14.1 Getting Degree Measures

full_no_zero_without_CAPS_indegree<-degree(full_no_zero_without_CAPS, mode = "in")
full_no_zero_without_CAPS_outdegree<-degree(full_no_zero_without_CAPS, mode = "out")
full_no_zero_without_CAPS_totaldegree<-degree(full_no_zero_without_CAPS, mode = "total")
full_no_zero_strength<- strength(full_no_zero_without_CAPS, weights=E(full_no_zero_without_CAPS)$weight)
full_no_zero_without_CAPS_indegree_n<-degree(full_no_zero_without_CAPS, mode = "in", normalized = T) %>% round(3)
full_no_zero_without_CAPS_outdegree_n<-degree(full_no_zero_without_CAPS, mode = "out", normalized = T) %>% round(3)
full_no_zero_without_CAPS_totaldegree_n<-degree(full_no_zero_without_CAPS, mode = "total", normalized = T) %>% round(3)
full_no_zero_centr_degree <- centralization.degree(full_no_zero_without_CAPS)$res
full_no_zero_without_CAPS_a.nn.deg <- graph.knn(full_no_zero_without_CAPS_simplified)$knn %>% round(1)
full_no_zero_without_CAPS_a.nn.deg_w <- graph.knn(full_no_zero_without_CAPS_simplified, weights=E(full_no_zero_without_CAPS_simplified)$weight)$knn %>% round(1)

14.2 Creating a dataframe of measures

full_no_zero_without_CAPS_df_degree <- data.frame(full_no_zero_without_CAPS_indegree,
full_no_zero_without_CAPS_outdegree, 
full_no_zero_without_CAPS_totaldegree,
full_no_zero_without_CAPS_indegree_n, 
full_no_zero_without_CAPS_outdegree_n,
full_no_zero_without_CAPS_totaldegree_n,
full_no_zero_strength,
full_no_zero_centr_degree,
full_no_zero_without_CAPS_a.nn.deg,
full_no_zero_without_CAPS_a.nn.deg_w) %>% round(3)

#Adding type
full_no_zero_without_CAPS_df_degree <-cbind(full_no_zero_without_CAPS_df_degree, V(full_no_zero_without_CAPS)$LABEL_COR)

#Adding names
names(full_no_zero_without_CAPS_df_degree) <- c("In Degree", "Out Degree", "Total Degree","In Degree Normalized", "Out Degree Normalized", "Total Degree Normalized", "Strength","Centralization Degree","Average Neighbor Degree","Average Weighted Neighbor Degree","Type")

#Ordering Variables
full_no_zero_without_CAPS_df_degree<-full_no_zero_without_CAPS_df_degree[c("Type","In Degree", "Out Degree", "Total Degree","In Degree Normalized", "Out Degree Normalized", "Total Degree Normalized", "Strength","Centralization Degree","Average Neighbor Degree","Average Weighted Neighbor Degree")]

14.3 General tabel - DT

datatable(full_no_zero_without_CAPS_df_degree, filter = 'top')

14.4 Aggregating data from previous table - mean

aggdata_mean <-aggregate(. ~ Type, full_no_zero_without_CAPS_df_degree, function(x) c(mean=mean(x)))

#Removing Type variable
names(aggdata_mean) <- c("Group", "In Degree(M)", "Out Degree(M)", "Total Degree(M)","In Degree Normalized(M)", "Out Degree Normalized(M)", "Total Degree Normalized(M)", "Strength(M)","Centralization Degree(M)","Average Neighbor Degree(M)","Average Weighted Neighbor Degree(M)")

14.5 Aggregating data from previous table - median

aggdata_median <-aggregate(. ~ Type, full_no_zero_without_CAPS_df_degree, function(x) c(median=median(x)))

#Removing Type variable
#aggdata_median<-aggdata_median[,-c(2)]
names(aggdata_median) <- c("Group", "In Degree(median)", "Out Degree(median)", "Total Degree(median)","In Degree Normalized(median)", "Out Degree Normalized(median)", "Total Degree Normalized(median)", "Strength(median)","Centralization Degree(median)","Average Neighbor Degree(median)","Average Weighted Neighbor Degree(median)")

14.6 Merging mean and median

total_table <- merge(aggdata_mean,aggdata_median,by="Group")

#Rounding
Group<-total_table[,c(1)] #Keeping group
total_table<-total_table[,-c(1)] %>% round(2) #Rouding
total_table<-cbind(Group,total_table) #Binding toghter

#Organizing Variabels
total_table<-total_table[c("Group","In Degree(M)","In Degree(median)", "Out Degree(M)", "Out Degree(median)","Total Degree(M)", "Total Degree(median)", "In Degree Normalized(M)", "In Degree Normalized(median)", "Out Degree Normalized(M)", "Out Degree Normalized(median)", "Total Degree Normalized(M)", "Total Degree Normalized(median)", "Strength(M)","Strength(median)", "Centralization Degree(M)","Centralization Degree(median)","Average Neighbor Degree(M)","Average Neighbor Degree(median)","Average Weighted Neighbor Degree(M)", "Average Weighted Neighbor Degree(median)")]

14.7 Plotting final table with round

datatable(total_table, filter = 'top')

14.8 Creating a dataframe of measures (Natureza Governamental)

full_no_zero_without_CAPS_df_degree <- data.frame(full_no_zero_without_CAPS_indegree,
full_no_zero_without_CAPS_outdegree, 
full_no_zero_without_CAPS_totaldegree,
full_no_zero_without_CAPS_indegree_n, 
full_no_zero_without_CAPS_outdegree_n,
full_no_zero_without_CAPS_totaldegree_n,
full_no_zero_strength,
full_no_zero_centr_degree,
full_no_zero_without_CAPS_a.nn.deg,
full_no_zero_without_CAPS_a.nn.deg_w) %>% round(3)

#Adding type
full_no_zero_without_CAPS_df_degree <-cbind(full_no_zero_without_CAPS_df_degree, V(full_no_zero_without_CAPS)$TIPO1)

#Adding names
names(full_no_zero_without_CAPS_df_degree) <- c("In Degree", "Out Degree", "Total Degree","In Degree Normalized", "Out Degree Normalized", "Total Degree Normalized", "Strength","Centralization Degree","Average Neighbor Degree","Average Weighted Neighbor Degree","Type")

#Ordering Variables
full_no_zero_without_CAPS_df_degree<-full_no_zero_without_CAPS_df_degree[c("Type","In Degree", "Out Degree", "Total Degree","In Degree Normalized", "Out Degree Normalized", "Total Degree Normalized", "Strength","Centralization Degree","Average Neighbor Degree","Average Weighted Neighbor Degree")]

14.9 General tabel - DT

datatable(full_no_zero_without_CAPS_df_degree, filter = 'top')

14.10 Aggregating data from previous table - mean

aggdata_mean <-aggregate(full_no_zero_without_CAPS_df_degree, by=list(full_no_zero_without_CAPS_df_degree$Type), FUN=mean, na.rm=TRUE)

#Removing Type variable
aggdata_mean<-aggdata_mean[,-c(2)]
names(aggdata_mean) <- c("Group", "In Degree(M)", "Out Degree(M)", "Total Degree(M)","In Degree Normalized(M)", "Out Degree Normalized(M)", "Total Degree Normalized(M)", "Strength(M)","Centralization Degree(M)","Average Neighbor Degree(M)","Average Weighted Neighbor Degree(M)")

14.11 Aggregating data from previous table - median

aggdata_median <-aggregate(. ~ Type, full_no_zero_without_CAPS_df_degree, function(x) c(median=median(x)))

#Removing Type variable
#aggdata_median<-aggdata_median[,-c(2)]
names(aggdata_median) <- c("Group", "In Degree(median)", "Out Degree(median)", "Total Degree(median)","In Degree Normalized(median)", "Out Degree Normalized(median)", "Total Degree Normalized(median)", "Strength(median)","Centralization Degree(median)","Average Neighbor Degree(median)","Average Weighted Neighbor Degree(median)")

14.12 Merging mean and median

total_table <- merge(aggdata_mean,aggdata_median,by="Group")

#Rounding
Group<-total_table[,c(1)] #Keeping group
total_table<-total_table[,-c(1)] %>% round(2) #Rouding
total_table<-cbind(Group,total_table) #Binding toghter

#Organizing Variabels
total_table<-total_table[c("Group","In Degree(M)","In Degree(median)", "Out Degree(M)", "Out Degree(median)","Total Degree(M)", "Total Degree(median)", "In Degree Normalized(M)", "In Degree Normalized(median)", "Out Degree Normalized(M)", "Out Degree Normalized(median)", "Total Degree Normalized(M)", "Total Degree Normalized(median)", "Strength(M)","Strength(median)", "Centralization Degree(M)","Centralization Degree(median)","Average Neighbor Degree(M)","Average Neighbor Degree(median)","Average Weighted Neighbor Degree(M)", "Average Weighted Neighbor Degree(median)")]

14.13 Plotting final table with round

datatable(total_table, filter = 'top')

14.14 Creating a dataframe of measures (Setores)

full_no_zero_without_CAPS_df_degree <- data.frame(full_no_zero_without_CAPS_indegree,
full_no_zero_without_CAPS_outdegree, 
full_no_zero_without_CAPS_totaldegree,
full_no_zero_without_CAPS_indegree_n, 
full_no_zero_without_CAPS_outdegree_n,
full_no_zero_without_CAPS_totaldegree_n,
full_no_zero_strength,
full_no_zero_centr_degree,
full_no_zero_without_CAPS_a.nn.deg,
full_no_zero_without_CAPS_a.nn.deg_w) %>% round(3)

#Adding type
full_no_zero_without_CAPS_df_degree <-cbind(full_no_zero_without_CAPS_df_degree, V(full_no_zero_without_CAPS)$TIPO2)

#Adding names
names(full_no_zero_without_CAPS_df_degree) <- c("In Degree", "Out Degree", "Total Degree","In Degree Normalized", "Out Degree Normalized", "Total Degree Normalized", "Strength","Centralization Degree","Average Neighbor Degree","Average Weighted Neighbor Degree","Type")

#Ordering Variables
full_no_zero_without_CAPS_df_degree<-full_no_zero_without_CAPS_df_degree[c("Type","In Degree", "Out Degree", "Total Degree","In Degree Normalized", "Out Degree Normalized", "Total Degree Normalized", "Strength","Centralization Degree","Average Neighbor Degree","Average Weighted Neighbor Degree")]

14.15 General tabel - DT

datatable(full_no_zero_without_CAPS_df_degree, filter = 'top')

14.16 Aggregating data from previous table - mean

aggdata_mean <-aggregate(full_no_zero_without_CAPS_df_degree, by=list(full_no_zero_without_CAPS_df_degree$Type), FUN=mean, na.rm=TRUE)

#Removing Type variable
aggdata_mean<-aggdata_mean[,-c(2)]
names(aggdata_mean) <- c("Group", "In Degree(M)", "Out Degree(M)", "Total Degree(M)","In Degree Normalized(M)", "Out Degree Normalized(M)", "Total Degree Normalized(M)", "Strength(M)","Centralization Degree(M)","Average Neighbor Degree(M)","Average Weighted Neighbor Degree(M)")

14.17 Aggregating data from previous table - median

aggdata_median <-aggregate(. ~ Type, full_no_zero_without_CAPS_df_degree, function(x) c(median=median(x)))

#Removing Type variable
#aggdata_median<-aggdata_median[,-c(2)]
names(aggdata_median) <- c("Group", "In Degree(median)", "Out Degree(median)", "Total Degree(median)","In Degree Normalized(median)", "Out Degree Normalized(median)", "Total Degree Normalized(median)", "Strength(median)","Centralization Degree(median)","Average Neighbor Degree(median)","Average Weighted Neighbor Degree(median)")

14.18 Merging mean and median

total_table <- merge(aggdata_mean,aggdata_median,by="Group")

#Rounding
Group<-total_table[,c(1)] #Keeping group
total_table<-total_table[,-c(1)] %>% round(2) #Rouding
total_table<-cbind(Group,total_table) #Binding toghter

#Organizing Variabels
total_table<-total_table[c("Group","In Degree(M)","In Degree(median)", "Out Degree(M)", "Out Degree(median)","Total Degree(M)", "Total Degree(median)", "In Degree Normalized(M)", "In Degree Normalized(median)", "Out Degree Normalized(M)", "Out Degree Normalized(median)", "Total Degree Normalized(M)", "Total Degree Normalized(median)", "Strength(M)","Strength(median)", "Centralization Degree(M)","Centralization Degree(median)","Average Neighbor Degree(M)","Average Neighbor Degree(median)","Average Weighted Neighbor Degree(M)", "Average Weighted Neighbor Degree(median)")]

14.19 Plotting final table with round

datatable(total_table, filter = 'top')

15 Network plotting based only on degree measures

#Set Seed
set.seed(123)
#Plotting based only on degree measures 
edge.start <- ends(full_no_zero_without_CAPS, es=E(full_no_zero_without_CAPS), names=F)[,1]

# Fixing ego
minC <- rep(-Inf, vcount(full_no_zero_without_CAPS))
maxC <- rep(Inf, vcount(full_no_zero_without_CAPS))
minC[1] <- maxC[1] <- 0
co <- layout_with_fr(full_no_zero_without_CAPS, niter=10000, minx=minC, maxx=maxC,miny=minC, maxy=maxC, weights = E(full_no_zero_without_CAPS)$weight)

#PLotting
plot(full_no_zero_without_CAPS, 
     layout=co,
     edge.color=V(full_no_zero_without_CAPS)$color[edge.start],
     edge.arrow.size=(degree(full_no_zero_without_CAPS)+1)/(100*mean(degree(full_no_zero_without_CAPS))),
     edge.width=E(full_no_zero_without_CAPS)$weight/(10*mean(E(full_no_zero_without_CAPS)$weight)),
     edge.curved = TRUE,
     vertex.size=log((degree(full_no_zero_without_CAPS)+2))*(0.5*mean(degree(full_no_zero_without_CAPS))),
     vertex.frame.color="#ffffff",
     vertex.label.color="black",
     vertex.label=get.vertex.attribute(full_no_zero_without_CAPS,"LABEL_COR"),
     vertex.label.cex=log(degree(full_no_zero_without_CAPS)+2)/mean(degree(full_no_zero_without_CAPS)),
     vertex.label.dist=0,
     rescale=F,
     xlim=range(co[,1]), 
     ylim=range(co[,2]))
axis(1)
axis(2)


#Solving Problems with legend rendering 
a<-V(full_no_zero_without_CAPS)$LABEL_COR
b<-V(full_no_zero_without_CAPS)$color
c<-table(a,b)
d<-as.data.frame(c)
e<-subset(d, d$Freq>0)
f<-t(e$a)
g<-t(e$b)

#Adding Legend
legend(x=range(co[,1])[2], y=range(co[,2])[2],
       legend=as.character(f),
       pch=21,
       col = "#777777", 
       pt.bg=as.character(g),
       pt.cex=2,
       bty="n", 
       ncol=1,
       lty=1,
       cex = .5)

#Adding Title
  title("Network Vertex Degree Sized - fuull_no_zero_fancy", sub = "Source: from authors ", cex = .5)
  text(x=range(co[,1])[1], y=range(co[,2])[1], labels = 
   sprintf("Median In Degree: %.2f\n Median Out Degree: %.2f",
     median(degree(full_no_zero_without_CAPS, mode="in")), 
     median(degree(full_no_zero_without_CAPS, mode="out"))
   ))

16 Network plotting based only on degree measures

#Set Seed
set.seed(123)

#Get Variable
V(full_no_zero_without_CAPS)$full_no_zero_color_degree<-V(full_no_zero_without_CAPS)$full_no_zero_totaldegree %>% round(1)

#Creating brewer pallette
full_no_zero_vertex_color_degree<-
  colorRampPalette(brewer.pal(length(unique(
          V(full_no_zero_without_CAPS)$full_no_zero_color_degree)), "RdBu"))(
            length(unique(V(full_no_zero_without_CAPS)$full_no_zero_color_degree)))

#Saving as Vertex properties 
V(full_no_zero_without_CAPS)$full_no_zero_vertex_color_degree<- full_no_zero_vertex_color_degree[as.numeric(cut(degree(full_no_zero_without_CAPS),breaks =length(unique(V(full_no_zero_without_CAPS)$full_no_zero_color_degree))))]

set.seed(123)
#Plotting based only on degree measures 
edge.start <- ends(full_no_zero_without_CAPS, es=E(full_no_zero_without_CAPS), names=F)[,1]

# Fixing ego
minC <- rep(-Inf, vcount(full_no_zero_without_CAPS))
maxC <- rep(Inf, vcount(full_no_zero_without_CAPS))
minC[1] <- maxC[1] <- 0
co <- layout_with_fr(full_no_zero_without_CAPS, niter=10000, minx=minC, maxx=maxC,miny=minC, maxy=maxC, weights = E(full_no_zero_without_CAPS)$weight)

#PLotting
plot(full_no_zero_without_CAPS, 
     layout=co,
     #edge.color=V(full_no_zero_without_CAPS)$color[edge.start],
     edge.arrow.size=(degree(full_no_zero_without_CAPS)+1)/3000,
     edge.width=E(full_no_zero_without_CAPS)$weight/10,
     edge.curved = TRUE,
     vertex.color=V(full_no_zero_without_CAPS)$full_no_zero_vertex_color_degree,
     vertex.size=log((degree(full_no_zero_without_CAPS)+2))*10,
     vertex.size=20,
     vertex.frame.color="#ffffff",
     vertex.label.color="black",
     vertex.label=get.vertex.attribute(full_no_zero_without_CAPS,"LABEL_COR"),
     vertex.label.cex=log((degree(full_no_zero_without_CAPS)+2))/10,
     vertex.label.dist=0,
     rescale=F,
     xlim=range(co[,1]), 
     ylim=range(co[,2])
     )
axis(1)
axis(2)


#Solving Problems with legend rendering 
a<-V(full_no_zero_without_CAPS)$full_no_zero_color_degree
b<-V(full_no_zero_without_CAPS)$full_no_zero_vertex_color_degree
c<-table(a,b)
d<-as.data.frame(c)
e<-subset(d, d$Freq>0)
e<-e[order(e$a,decreasing=T),] 
f<-t(e$a)
g<-t(e$b)

#Adding Legend
legend(x=range(co[,1])[2], y=range(co[,2])[2],
       legend=as.character(f),
       pch=21,
       col = "#777777", 
       pt.bg=as.character(g),
       pt.cex=2,
       bty="n", 
       ncol=1,
       lty=1,
       cex = .3)

#Adding Title
  title("Network Vertex Degree Sized and Red to Blue - fuull_no_zero_fancy", sub = "Source: from authors ")
  text(x=range(co[,1])[1], y=range(co[,2])[1], labels = 
   sprintf("Median In Degree: %.2f\nMedian Out Degree: %.2f",
     median(degree(full_no_zero_without_CAPS, mode="in")), 
     median(degree(full_no_zero_without_CAPS, mode="out"))
   ))

17 Network Plotting Centralization - Degree Measures - Using Spectral Color as Distance Measure Representation

#Set Seed
set.seed(123)

#Get Variable
V(full_no_zero_without_CAPS)$full_no_zero_color_degree<-V(full_no_zero_without_CAPS)$full_no_zero_centr_degree

#Creating brewer pallette
full_no_zero_vertex_color_degree<-
  colorRampPalette(brewer.pal(length(unique(
          V(full_no_zero_without_CAPS)$full_no_zero_color_degree)), "Spectral"))(
            length(unique(V(full_no_zero_without_CAPS)$full_no_zero_color_degree)))

#Saving as Vertex properties 
V(full_no_zero_without_CAPS)$full_no_zero_vertex_color_degree<- full_no_zero_vertex_color_degree[as.numeric(cut(V(full_no_zero_without_CAPS)$full_no_zero_color_degree,breaks =length(unique(V(full_no_zero_without_CAPS)$full_no_zero_color_degree))))]

#Plotting based only on degree measures 
edge.start <- ends(full_no_zero_without_CAPS, es=E(full_no_zero_without_CAPS), names=F)[,1]

# Fixing ego
minC <- rep(-Inf, vcount(full_no_zero_without_CAPS))
maxC <- rep(Inf, vcount(full_no_zero_without_CAPS))
minC[1] <- maxC[1] <- 0
co <- layout_with_fr(full_no_zero_without_CAPS, niter=10000, minx=minC, maxx=maxC,miny=minC, maxy=maxC, weights = E(full_no_zero_without_CAPS)$weight)

#PLotting
plot(full_no_zero_without_CAPS, 
     layout=co,
     edge.color=V(full_no_zero_without_CAPS)$full_no_zero_vertex_color_degree[edge.start],
     edge.arrow.size=(degree(full_no_zero_without_CAPS)+1)/10000,
     edge.width=E(full_no_zero_without_CAPS)$weight/10,
     edge.curved = TRUE,
     vertex.color=V(full_no_zero_without_CAPS)$full_no_zero_vertex_color_degree,
     vertex.size=log((V(full_no_zero_without_CAPS)$full_no_zero_centr_degree+2))*10,
     vertex.frame.color="#ffffff",
     vertex.label.color="black",
     vertex.label=get.vertex.attribute(full_no_zero_without_CAPS,"LABEL_COR"),
     vertex.label.cex=log((degree(full_no_zero_without_CAPS)+2))/10,
     vertex.label.dist=0,
     rescale=F,
     xlim=range(co[,1]), 
     ylim=range(co[,2]))
axis(1)
axis(2)


#Solving Problems with legend rendering 
a<-V(full_no_zero_without_CAPS)$full_no_zero_color_degree
b<-V(full_no_zero_without_CAPS)$full_no_zero_vertex_color_degree
c<-table(a,b)
d<-as.data.frame(c)
e<-subset(d, d$Freq>0)
e<-e[order(e$a,decreasing=T),] 
f<-t(e$a)
g<-t(e$b)

#Adding Legend
legend(x=range(co[,1])[2], y=range(co[,2])[2],
       legend=as.character(f),
       pch=21,
       col = "#777777", 
       pt.bg=as.character(g),
       pt.cex=2,
       bty="n", 
       ncol=1,
       lty=1,
       cex = .3)

#Adding Title
  title("Network Vertex Centralization Degree Sized Spectral Colored - fuull_no_zero_fancy", sub = "Source: from authors ")
  text(x=range(co[,1])[1], y=range(co[,2])[1], labels = 
   sprintf("Median In Degree: %.2f\nMedian Out Degree: %.2f",
     median(degree(full_no_zero_without_CAPS, mode="in")), 
     median(degree(full_no_zero_without_CAPS, mode="out"))
   ))

18 Alternative vizualization using degree in order to genarete sub-graphs - Higher than median degree network

#Set Seed
set.seed(123)

# Network elements with lower than meadian degree
higherthanmedian.network_full_no_zero_without_CAPS<-V(full_no_zero_without_CAPS)[degree(full_no_zero_without_CAPS)<median(degree(full_no_zero_without_CAPS))] 

#Deleting vertices based in intersection betewenn full_no_zero_without_CAPS 
high_full_no_zero_without_CAPS<-delete.vertices(full_no_zero_without_CAPS, higherthanmedian.network_full_no_zero_without_CAPS)

#Plotting based only on degree measures 
edge.start <- ends(high_full_no_zero_without_CAPS, es=E(high_full_no_zero_without_CAPS), names=F)[,1]

# Fixing ego
minC <- rep(-Inf, vcount(high_full_no_zero_without_CAPS))
maxC <- rep(Inf, vcount(high_full_no_zero_without_CAPS))
minC[1] <- maxC[1] <- 0
co <- layout_with_fr(high_full_no_zero_without_CAPS, niter=10000, minx=minC, maxx=maxC,miny=minC, maxy=maxC, weights = E(high_full_no_zero_without_CAPS)$weight)

#PLotting
plot(high_full_no_zero_without_CAPS, 
     layout=co,
     edge.color=V(high_full_no_zero_without_CAPS)$color[edge.start],
     edge.arrow.size=(degree(high_full_no_zero_without_CAPS)+1)/1000,
     edge.width=E(high_full_no_zero_without_CAPS)$weight/10,
     edge.curved = TRUE,
     vertex.size=log((V(high_full_no_zero_without_CAPS)$full_no_zero_centr_degree+2))*10,
     vertex.frame.color="#ffffff",
     vertex.label.color="black",
     vertex.label=get.vertex.attribute(high_full_no_zero_without_CAPS,"LABEL_COR"),
     vertex.label.cex=log((degree(high_full_no_zero_without_CAPS)+2))/10,
     vertex.label.dist=0,
     rescale=F,
     xlim=range(co[,1]), 
     ylim=range(co[,2]))
axis(1)
axis(2)


#Solving Problems with legend rendering 
a<-V(high_full_no_zero_without_CAPS)$LABEL_COR
b<-V(high_full_no_zero_without_CAPS)$color
c<-table(a,b)
d<-as.data.frame(c)
e<-subset(d, d$Freq>0)
f<-t(e$a)
g<-t(e$b)

#Adding Legend
legend(x=range(co[,1])[2], y=range(co[,2])[2],
       legend=as.character(f),
       pch=21,
       col = "#777777", 
       pt.bg=as.character(g),
       pt.cex=3,
       bty="n", 
       ncol=1,
       lty=1,
       cex = .5)

#Adding Title
  title("Network Higher Than Median Degree - fuull_no_zero_fancy", sub = "Source: from authors ")
  text(x=range(co[,1])[1], y=range(co[,2])[1], labels = 
   sprintf("Mean In Degree: %.2f\n Mean Out Degree: %.2f",
     mean(degree(high_full_no_zero_without_CAPS, mode="in")), 
     mean(degree(high_full_no_zero_without_CAPS, mode="out"))
   )
  )

19 Alternative vizualization using degree in order to genarete sub-graphs - Lower than median degree network

#Set Seed
set.seed(123)

# Network elements with lower than meadian degree
lowerthanmedian.network_full_no_zero_without_CAPS<-V(full_no_zero_without_CAPS)[degree(full_no_zero_without_CAPS)>median(degree(full_no_zero_without_CAPS))] 

#Deleting vertices based in intersection betewenn full_no_zero_without_CAPS 
small_full_no_zero_without_CAPS<-delete.vertices(full_no_zero_without_CAPS, lowerthanmedian.network_full_no_zero_without_CAPS)

#Plotting based only on degree measures 
edge.start <- ends(small_full_no_zero_without_CAPS, es=E(small_full_no_zero_without_CAPS), names=F)[,1]

# Fixing ego
minC <- rep(-Inf, vcount(small_full_no_zero_without_CAPS))
maxC <- rep(Inf, vcount(small_full_no_zero_without_CAPS))
minC[1] <- maxC[1] <- 0
co <- layout_with_fr(small_full_no_zero_without_CAPS, niter=10000, minx=minC, maxx=maxC,miny=minC, maxy=maxC, weights = E(small_full_no_zero_without_CAPS)$weight)

#PLotting
plot(small_full_no_zero_without_CAPS, 
     layout=co,
     edge.color=V(small_full_no_zero_without_CAPS)$color[edge.start],
     edge.arrow.size=(degree(small_full_no_zero_without_CAPS)+1)/1000,
     edge.width=E(small_full_no_zero_without_CAPS)$weight/10,
     edge.curved = TRUE,
     vertex.size=log((V(small_full_no_zero_without_CAPS)$full_no_zero_centr_degree+2))*20,
     vertex.frame.color="#ffffff",
     vertex.label.color="black",
     vertex.label=get.vertex.attribute(small_full_no_zero_without_CAPS,"LABEL_COR"),
     vertex.label.cex=log((degree(small_full_no_zero_without_CAPS)+2))/3,
     vertex.label.dist=0,
     rescale=F,
     xlim=range(co[,1]), 
     ylim=range(co[,2]))
axis(1)
axis(2)

#Solving Problems with legend rendering 
a<-V(small_full_no_zero_without_CAPS)$LABEL_COR
b<-V(small_full_no_zero_without_CAPS)$color
c<-table(a,b)
d<-as.data.frame(c)
e<-subset(d, d$Freq>0)
f<-t(e$a)
g<-t(e$b)

#Adding Legend
legend(x=range(co[,1])[2], y=range(co[,2])[2],
       legend=as.character(f),
       pch=21,
       col = "#777777", 
       pt.bg=as.character(g),
       pt.cex=4,
       bty="n", 
       ncol=1,
       lty=1,
       cex = .5)

#Adding Title
  title("Network Smaller Than Median Degree - fuull_no_zero_fancy", sub = "Source: from authors ")
  text(x=range(co[,1])[1], y=range(co[,2])[1], labels = 
   sprintf("Mean In Degree: %.2f\nMean Out Degree: %.2f",
     mean(degree(small_full_no_zero_without_CAPS, mode="in")), 
     mean(degree(small_full_no_zero_without_CAPS, mode="out"))
   )
  )

20 Plotting using Average Neighbor Degree

#Set Seed
set.seed(123)

#Plotting based only on degree measures 
edge.start <- ends(full_no_zero_without_CAPS_simplified, es=E(full_no_zero_without_CAPS_simplified), names=F)[,1]

# Fixing ego
minC <- rep(-Inf, vcount(full_no_zero_without_CAPS_simplified))
maxC <- rep(Inf, vcount(full_no_zero_without_CAPS_simplified))
minC[1] <- maxC[1] <- 0
co <- layout_with_fr(full_no_zero_without_CAPS_simplified, niter=10000, minx=minC, maxx=maxC,miny=minC, maxy=maxC, weights = E(full_no_zero_without_CAPS_simplified)$weight)

#Plotting based only on degree measures  #full_no_zero_without_CAPS_simplified_a.nn.deg
V(full_no_zero_without_CAPS_simplified)$full_no_zero_without_CAPS_a.nn.deg<-as.numeric(graph.knn(full_no_zero_without_CAPS_simplified)$knn)
V(full_no_zero_without_CAPS_simplified)$full_no_zero_without_CAPS_a.nn.deg[V(full_no_zero_without_CAPS_simplified)$full_no_zero_without_CAPS_a.nn.deg=="NaN"]<-0

#PLotting
plot(full_no_zero_without_CAPS_simplified, 
     layout=co,
     edge.color=V(full_no_zero_without_CAPS_simplified)$color[edge.start],
     edge.arrow.size=sqrt((V(full_no_zero_without_CAPS_simplified)$full_no_zero_without_CAPS_a.nn.deg)^2+1)/1000,
     edge.width=E(full_no_zero_without_CAPS_simplified)$weight/80,
     edge.curved = TRUE,
     vertex.color=V(full_no_zero_without_CAPS_simplified)$color,
     vertex.size=(sqrt((V(full_no_zero_without_CAPS_simplified)$full_no_zero_without_CAPS_a.nn.deg)^2))/1,
     vertex.frame.color="#ffffff",
     vertex.label.color="black",
     vertex.label=get.vertex.attribute(full_no_zero_without_CAPS_simplified,"LABEL_COR"),
     vertex.label.cex=(sqrt((V(full_no_zero_without_CAPS_simplified)$full_no_zero_without_CAPS_a.nn.deg)^2)+1)/250,
     vertex.label.dist=0,
     rescale=F,
     xlim=range(co[,1]), 
     ylim=range(co[,2]))
axis(1)
axis(2)


#Solving Problems with legend rendering 
a<-V(full_no_zero_without_CAPS_simplified)$LABEL_COR
b<-V(full_no_zero_without_CAPS_simplified)$color
c<-table(a,b)
d<-as.data.frame(c)
e<-subset(d, d$Freq>0)
f<-t(e$a)
g<-t(e$b)

#Adding Legend
legend(x=range(co[,1])[2], 
       y=range(co[,2])[2],
       legend=as.character(f),
       inset=2,
       pch=21,
       col = "#777777", 
       pt.bg=as.character(g),
       pt.cex=2,
       bty="n", 
       ncol=1,
       lty=3,
       lwd=2,
       x.intersp=2,
       y.intersp=4,
       cex = .3)

#Adding Title
  title("Network Average Neighbor Degree Sized - fuull_no_zero_fancy", sub = "Source: from authors ")
  text(x=range(co[,1])[1], y=range(co[,2])[1], labels = 
   sprintf("Median Average Neighbor Degree: %.2f",
     median((full_no_zero_without_CAPS_a.nn.deg+1))
   ))

21 Plotting using Average Neighbor Degree

#Set Seed
set.seed(123)

#Plotting based only on degree measures 
edge.start <- ends(full_no_zero_without_CAPS_simplified, es=E(full_no_zero_without_CAPS_simplified), names=F)[,1]

# Fixing ego
minC <- rep(-Inf, vcount(full_no_zero_without_CAPS_simplified))
maxC <- rep(Inf, vcount(full_no_zero_without_CAPS_simplified))
minC[1] <- maxC[1] <- 0
co <- layout_with_fr(full_no_zero_without_CAPS_simplified, niter=10000, minx=minC, maxx=maxC,miny=minC, maxy=maxC, weights = E(full_no_zero_without_CAPS_simplified)$weight)


#Plotting based only on degree measures  #full_no_zero_without_CAPS_a.nn.deg
V(full_no_zero_without_CAPS_simplified)$full_no_zero_without_CAPS_a.nn.deg_w<-as.numeric(graph.knn(full_no_zero_without_CAPS_simplified, weights = E(full_no_zero_without_CAPS_simplified)$weight)$knn)
V(full_no_zero_without_CAPS_simplified)$full_no_zero_without_CAPS_a.nn.deg_w[V(full_no_zero_without_CAPS_simplified)$full_no_zero_without_CAPS_a.nn.deg_w=="NaN"]<-0

#PLotting
plot(full_no_zero_without_CAPS_simplified, 
     layout=co,
     edge.color=V(full_no_zero_without_CAPS_simplified)$color[edge.start],
     edge.arrow.size=sqrt((V(full_no_zero_without_CAPS_simplified)$full_no_zero_without_CAPS_a.nn.deg_w)^2+1)/1000,
     edge.width=E(full_no_zero_without_CAPS_simplified)$weight/100,
     edge.curved = TRUE,
     vertex.color=V(full_no_zero_without_CAPS_simplified)$color,
     vertex.size=(sqrt((V(full_no_zero_without_CAPS_simplified)$full_no_zero_without_CAPS_a.nn.deg_w)^2))/5,
     vertex.frame.color="#ffffff",
     vertex.label.color="black",
     vertex.label=get.vertex.attribute(full_no_zero_without_CAPS_simplified,"LABEL_COR"),
     vertex.label.cex=(sqrt((V(full_no_zero_without_CAPS_simplified)$full_no_zero_without_CAPS_a.nn.deg_w)^2)+1)/500,
     vertex.label.dist=0,
     rescale=F,
     xlim=range(co[,1]), 
     ylim=range(co[,2]))
axis(1)
axis(2)


#Solving Problems with legend rendering 
a<-V(full_no_zero_without_CAPS_simplified)$LABEL_COR
b<-V(full_no_zero_without_CAPS_simplified)$color
c<-table(a,b)
d<-as.data.frame(c)
e<-subset(d, d$Freq>0)
f<-t(e$a)
g<-t(e$b)

#Adding Legend
legend(x=range(co[,1])[2], y=range(co[,2])[2],
       legend=as.character(f),
       pch=21,
       col = "#777777", 
       pt.bg=as.character(g),
       pt.cex=4,
       bty="n", 
       ncol=1,
       lty=1,
       cex = .5)

#Adding Title
  title("Network Average Weighted Neighbor Degree Sized - fuull_no_zero_fancy", sub = "Source: from authors ")
  text(x=range(co[,1])[1], y=range(co[,2])[1], labels = 
   sprintf("Median Average Weighted Neighbor Degree: %.2f",
     median((full_no_zero_without_CAPS_a.nn.deg_w+1))
   ))

22 Circle Degree Too intensive computation #code

#Circle Degree ***Too intense computation***
#A_full_no_zero_without_CAPS <- get.adjacency(full_no_zero_without_CAPS, sparse=FALSE)
#detach("package:igraph", unload=TRUE)
#library(network)
#g <- network::as.network.matrix(A_full_no_zero_without_CAPS)
#library(sna)
#gplot.target(g, degree(g), main="Circle Degree")
#library(igraph)

23 Closeness - centrality based on distance to others in the graph

How close an actor to all the other actors in network?

High closeness centrality - short communication path to others, minimal number of steps to reach others.

Answers the “Kevin Bacon” question:

How many steps are required to access every other vertex from a given vertex?

One practical implication of this metric: it helps you gauge how information might spread within your network, and who might be the best people to leverage if you need to make sure information gets around. Link here: http://www.tc.umn.edu/~alink/R-social-network-analysis.html

Closeness centrality can be defined as a measure of how far other nodes are from the node in question. Nodes with high closeness centrality are likely to be relatively efficient in receiving or transmitting information to/from distant parts of the social network.

Scores may be interpreted as arising from a reciprocal process in which the centrality of each actor is proportional to the sum of the centralities of those actors to whom he or she is connected.

In general, vertices with high eigenvector centralities are those which are connected to many other vertices which are, in turn, connected to many others (and so on). (The perceptive may realize that this implies that the largest values will be obtained by individuals in large cliques (or high-density substructures)

23.1 Closeness Non-normalized

23.1.1 Saving to Igraph object

V(full_no_zero_without_CAPS)$full_no_zero_incloseness <- closeness(full_no_zero_without_CAPS, mode = "in", weights = E(full_no_zero_without_CAPS)$full_no_zero) %>% round(6)
V(full_no_zero_without_CAPS)$full_no_zero_outcloseness <- closeness(full_no_zero_without_CAPS, mode = "out", weights = E(full_no_zero_without_CAPS)$full_no_zero) %>% round(6)
V(full_no_zero_without_CAPS)$full_no_zero_totalcloseness <- closeness(full_no_zero_without_CAPS, mode = "total", weights = E(full_no_zero_without_CAPS)$full_no_zero) %>% round(4)

23.1.2 Saving to Environment

full_no_zero_without_CAPS_incloseness<- closeness(full_no_zero_without_CAPS, mode = "in", weights = E(full_no_zero_without_CAPS)$full_no_zero) %>% round(6)
full_no_zero_without_CAPS_outcloseness<- closeness(full_no_zero_without_CAPS, mode = "out", weights = E(full_no_zero_without_CAPS)$full_no_zero) %>% round(6)
full_no_zero_without_CAPS_totalcloseness<- closeness(full_no_zero_without_CAPS, mode = "total", weights = E(full_no_zero_without_CAPS)$full_no_zero) %>% round(6)

23.2 Closeness Non-normalized - IN

summary(full_no_zero_without_CAPS_incloseness)
##      Min.   1st Qu.    Median      Mean   3rd Qu.      Max. 
## 0.0000290 0.0001100 0.0001100 0.0001093 0.0001110 0.0001160
sd(full_no_zero_without_CAPS_incloseness)
## [1] 8.487822e-06

23.2.1 Network Plotting Based On Non-normalized Closeness - IN

V(full_no_zero_without_CAPS)$full_no_zero_incloseness<-closeness(full_no_zero_without_CAPS, weights = E(full_no_zero_without_CAPS)$full_no_zero, mode="in")

#Get Variable
V(full_no_zero_without_CAPS)$full_no_zero_color_degree<-round(V(full_no_zero_without_CAPS)$full_no_zero_incloseness,6)

#Creating brewer pallette
full_no_zero_vertex_color_degree<-
  colorRampPalette(brewer.pal(length(unique(
          V(full_no_zero_without_CAPS)$full_no_zero_color_degree)), "RdBu"))(
            length(unique(V(full_no_zero_without_CAPS)$full_no_zero_color_degree)))

#Saving as Vertex properties 
V(full_no_zero_without_CAPS)$full_no_zero_vertex_color_degree<-
  full_no_zero_vertex_color_degree[as.numeric(
  cut(V(full_no_zero_without_CAPS)$full_no_zero_color_degree,
      breaks=length(unique(V(full_no_zero_without_CAPS)$full_no_zero_color_degree))))]

set.seed(123)
#Plotting based only on degree measures 
edge.start <- ends(full_no_zero_without_CAPS, es=E(full_no_zero_without_CAPS), names=F)[,1]

# Fixing ego
minC <- rep(-Inf, vcount(full_no_zero_without_CAPS))
maxC <- rep(Inf, vcount(full_no_zero_without_CAPS))
minC[1] <- maxC[1] <- 0
co <- layout_with_fr(full_no_zero_without_CAPS, niter=10^4, minx=minC, maxx=maxC,miny=minC, maxy=maxC, weights = E(full_no_zero_without_CAPS)$weight)


#PLotting
plot(full_no_zero_without_CAPS, 
     layout=co,
     edge.color=V(full_no_zero_without_CAPS)$full_no_zero_vertex_color_degree[edge.start],
     edge.arrow.size=closeness(full_no_zero_without_CAPS, weights = E(full_no_zero_without_CAPS)$full_no_zero, mode="in"),
     edge.width=E(full_no_zero_without_CAPS)$weight/mean(E(full_no_zero_without_CAPS)$weight),
     edge.curved = TRUE,
     vertex.color=V(full_no_zero_without_CAPS)$full_no_zero_vertex_color_degree,
     vertex.size=closeness(full_no_zero_without_CAPS, weights = E(full_no_zero_without_CAPS)$full_no_zero, mode="in")*10^5,
     vertex.frame.color="black",
     vertex.label.color="black",
     vertex.label=get.vertex.attribute(full_no_zero_without_CAPS,"LABEL_COR"),
     vertex.label.cex=(closeness(full_no_zero_without_CAPS, weights = E(full_no_zero_without_CAPS)$full_no_zero, mode="in")+10^-5)*2000,
     vertex.label.dist=0,
     rescale=F,
     xlim=range(co[,1]), 
     ylim=range(co[,2])
     )
axis(1)
axis(2)


#Solving Problems with legend rendering 
a<-V(full_no_zero_without_CAPS)$full_no_zero_color_degree
b<-V(full_no_zero_without_CAPS)$full_no_zero_vertex_color_degree
c<-table(a,b)
d<-as.data.frame(c)
e<-subset(d, d$Freq>0)
e<-e[order(e$a,decreasing=T),] 
f<-t(e$a)
g<-t(e$b)

#Adding Legend
legend(x=range(co[,1])[2], 
       y=range(co[,2])[2],
       legend=as.character(f),
       pch=21,
       col = "#777777", 
       pt.bg=as.character(g),
       pt.cex=2,
       bty="n", 
       ncol=1,
       lty=1,
       cex = .3)

#Adding Title
  title("Network Closeness Degree Sized and Colored In - fuull_no_zero_fancy", sub = "Source: from authors ")
  text( 
    x=range(co[,1])[1],
    y=range(co[,2])[1], 
      labels = sprintf(
             "Median In Closennes:%.4f\nSD In Closennes: %.5f",
             median(closeness(full_no_zero_without_CAPS, mode="in", weights = E(full_no_zero_without_CAPS)$full_no_zero)), 
             sd(closeness(full_no_zero_without_CAPS, mode="in", weights = E(full_no_zero_without_CAPS)$full_no_zero))
             )
       )

23.3 Closeness Non-normalized - OUT

summary(full_no_zero_without_CAPS_outcloseness)
##      Min.   1st Qu.    Median      Mean   3rd Qu.      Max. 
## 0.0000290 0.0002498 0.0011020 0.0008712 0.0012150 0.0014600
sd(full_no_zero_without_CAPS_outcloseness)
## [1] 0.0005014774

23.3.1 Network Plotting Based On Non-normalized Closeness - OUT

V(full_no_zero_without_CAPS)$full_no_zero_outcloseness<-closeness(full_no_zero_without_CAPS, weights = E(full_no_zero_without_CAPS)$full_no_zero, mode="out")

#Get Variable
V(full_no_zero_without_CAPS)$full_no_zero_color_degree<-round(V(full_no_zero_without_CAPS)$full_no_zero_outcloseness,6)

#Creating brewer pallette
full_no_zero_vertex_color_degree<-
  colorRampPalette(brewer.pal(length(unique(
          V(full_no_zero_without_CAPS)$full_no_zero_color_degree)), "RdBu"))(
            length(unique(V(full_no_zero_without_CAPS)$full_no_zero_color_degree)))

#Saving as Vertex properties 
V(full_no_zero_without_CAPS)$full_no_zero_vertex_color_degree<-
  full_no_zero_vertex_color_degree[as.numeric(
  cut(V(full_no_zero_without_CAPS)$full_no_zero_color_degree,
      breaks=length(unique(V(full_no_zero_without_CAPS)$full_no_zero_color_degree))))]

set.seed(123)
#Plotting based only on degree measures 
edge.start <- ends(full_no_zero_without_CAPS, es=E(full_no_zero_without_CAPS), names=F)[,1]

# Fixing ego
minC <- rep(-Inf, vcount(full_no_zero_without_CAPS))
maxC <- rep(Inf, vcount(full_no_zero_without_CAPS))
minC[1] <- maxC[1] <- 0
co <- layout_with_fr(full_no_zero_without_CAPS, niter=10^4, minx=minC, maxx=maxC,miny=minC, maxy=maxC, weights = E(full_no_zero_without_CAPS)$weight)


#PLotting
plot(full_no_zero_without_CAPS, 
     layout=co,
     edge.color=V(full_no_zero_without_CAPS)$full_no_zero_vertex_color_degree[edge.start],
     edge.arrow.size=closeness(full_no_zero_without_CAPS, weights = E(full_no_zero_without_CAPS)$full_no_zero, mode="out"),
     edge.width=E(full_no_zero_without_CAPS)$weight/2*mean(E(full_no_zero_without_CAPS)$weight),
     edge.curved = TRUE,
     vertex.color=V(full_no_zero_without_CAPS)$full_no_zero_vertex_color_degree,
     vertex.size=closeness(full_no_zero_without_CAPS, weights = E(full_no_zero_without_CAPS)$full_no_zero, mode="out")*10^4,
     vertex.frame.color="white",
     vertex.label.color="black",
     vertex.label=get.vertex.attribute(full_no_zero_without_CAPS,"LABEL_COR"),
     vertex.label.cex=closeness(full_no_zero_without_CAPS, weights = E(full_no_zero_without_CAPS)$full_no_zero, mode="out")*200,
     vertex.label.dist=0,
     rescale=F,
     xlim=range(co[,1]), 
     ylim=range(co[,2])
     )
axis(1)
axis(2)


#Solving Problems with legend rendering 
a<-V(full_no_zero_without_CAPS)$full_no_zero_color_degree
b<-V(full_no_zero_without_CAPS)$full_no_zero_vertex_color_degree
c<-table(a,b)
d<-as.data.frame(c)
e<-subset(d, d$Freq>0)
e<-e[order(e$a,decreasing=T),] 
f<-t(e$a)
g<-t(e$b)

#Adding Legend
legend(x=range(co[,1])[2], 
       y=range(co[,2])[2],
       legend=as.character(f),
       pch=21,
       col = "#777777", 
       pt.bg=as.character(g),
       pt.cex=2,
       bty="n", 
       ncol=1,
       lty=1,
       cex = .3)

#Adding Title
  title("Network Closeness Degree Sized and Colored OUT - fuull_no_zero_fancy", sub = "Source: from authors ")
  text( 
    x=range(co[,1])[1],
    y=range(co[,2])[1], 
      labels = sprintf(
             "Median OUT Closennes:%.4f\nSD OUT Closennes: %.5f",
             median(closeness(full_no_zero_without_CAPS, mode="out", weights = E(full_no_zero_without_CAPS)$full_no_zero)), 
             sd(closeness(full_no_zero_without_CAPS, mode="out", weights = E(full_no_zero_without_CAPS)$full_no_zero))
             )
       )

23.4 Closeness Non-normalized - ALL

summary(full_no_zero_without_CAPS_totalcloseness)
##     Min.  1st Qu.   Median     Mean  3rd Qu.     Max. 
## 0.000029 0.001534 0.001646 0.001620 0.001711 0.002227
sd(full_no_zero_without_CAPS_totalcloseness)
## [1] 0.0001930621

23.4.1 Network Plotting Based On Non-normalized Closeness - ALL

V(full_no_zero_without_CAPS)$full_no_zero_allcloseness<-closeness(full_no_zero_without_CAPS, weights = E(full_no_zero_without_CAPS)$full_no_zero, mode="all")

#Get Variable
V(full_no_zero_without_CAPS)$full_no_zero_color_degree<-round(V(full_no_zero_without_CAPS)$full_no_zero_allcloseness,6)

#Creating brewer pallette
full_no_zero_vertex_color_degree<-
  colorRampPalette(brewer.pal(length(unique(
          V(full_no_zero_without_CAPS)$full_no_zero_color_degree)), "RdBu"))(
            length(unique(V(full_no_zero_without_CAPS)$full_no_zero_color_degree)))

#Saving as Vertex properties 
V(full_no_zero_without_CAPS)$full_no_zero_vertex_color_degree<-
  full_no_zero_vertex_color_degree[as.numeric(
  cut(V(full_no_zero_without_CAPS)$full_no_zero_color_degree,
      breaks=length(unique(V(full_no_zero_without_CAPS)$full_no_zero_color_degree))))]

set.seed(123)
#Plotting based only on degree measures 
edge.start <- ends(full_no_zero_without_CAPS, es=E(full_no_zero_without_CAPS), names=F)[,1]

# Fixing ego
minC <- rep(-Inf, vcount(full_no_zero_without_CAPS))
maxC <- rep(Inf, vcount(full_no_zero_without_CAPS))
minC[1] <- maxC[1] <- 0
co <- layout_with_fr(full_no_zero_without_CAPS, niter=10^4, minx=minC, maxx=maxC,miny=minC, maxy=maxC, weights = E(full_no_zero_without_CAPS)$weight)


#PLotting
plot(full_no_zero_without_CAPS, 
     layout=co,
     edge.color=V(full_no_zero_without_CAPS)$full_no_zero_vertex_color_degree[edge.start],
     edge.arrow.size=closeness(full_no_zero_without_CAPS, weights = E(full_no_zero_without_CAPS)$full_no_zero, mode="all"),
     edge.width=E(full_no_zero_without_CAPS)$weight/2*mean(E(full_no_zero_without_CAPS)$weight),
     edge.curved = TRUE,
     vertex.color=V(full_no_zero_without_CAPS)$full_no_zero_vertex_color_degree,
     vertex.size=closeness(full_no_zero_without_CAPS, weights = E(full_no_zero_without_CAPS)$full_no_zero, mode="all")*10^4,
     vertex.frame.color="white",
     vertex.label.color="black",
     vertex.label=get.vertex.attribute(full_no_zero_without_CAPS,"LABEL_COR"),
     vertex.label.cex=(closeness(full_no_zero_without_CAPS, weights = E(full_no_zero_without_CAPS)$full_no_zero, mode="all")+0.00001)*200,
     vertex.label.dist=0,
     rescale=F,
     xlim=range(co[,1]), 
     ylim=range(co[,2])
     )
axis(1)
axis(2)


#Solving Problems with legend rendering 
a<-V(full_no_zero_without_CAPS)$full_no_zero_color_degree
b<-V(full_no_zero_without_CAPS)$full_no_zero_vertex_color_degree
c<-table(a,b)
d<-as.data.frame(c)
e<-subset(d, d$Freq>0)
e<-e[order(e$a,decreasing=T),] 
f<-t(e$a)
g<-t(e$b)

#Adding Legend
legend(x=range(co[,1])[2], 
       y=range(co[,2])[2],
       legend=as.character(f),
       pch=21,
       col = "#777777", 
       pt.bg=as.character(g),
       pt.cex=2,
       bty="n", 
       ncol=1,
       lty=1,
       cex = .3)

#Adding Title
  title("Network Closeness Degree Sized and Colored all - fuull_no_zero_fancy", sub = "Source: from authors ")
  text( 
    x=range(co[,1])[1],
    y=range(co[,2])[1], 
      labels = sprintf(
             "Median all Closennes:%.4f\nSD all Closennes: %.5f",
             median(closeness(full_no_zero_without_CAPS, mode="all", weights = E(full_no_zero_without_CAPS)$full_no_zero)), 
             sd(closeness(full_no_zero_without_CAPS, mode="all", weights = E(full_no_zero_without_CAPS)$full_no_zero))
             )
       )

23.5 Closeness Normalized

23.5.1 Saving to Igraph object

V(full_no_zero_without_CAPS)$full_no_zero_incloseness_n <- closeness(full_no_zero_without_CAPS, mode = "in",, weights = E(full_no_zero_without_CAPS)$full_no_zero, normalized = T) %>% round(10)
V(full_no_zero_without_CAPS)$full_no_zero_outcloseness_n <- closeness(full_no_zero_without_CAPS, mode = "out", normalized = T, weights = E(full_no_zero_without_CAPS)$full_no_zero) %>% round(6)
V(full_no_zero_without_CAPS)$full_no_zero_totalcloseness_n <- closeness(full_no_zero_without_CAPS, mode = "total", normalized = T, weights = E(full_no_zero_without_CAPS)$full_no_zero) %>% round(6)

23.5.2 Saving to Environment

full_no_zero_without_CAPS_incloseness_n<- closeness(full_no_zero_without_CAPS, mode = "in", normalized = T, weights = E(full_no_zero_without_CAPS)$full_no_zero) %>% round(6)
full_no_zero_without_CAPS_outcloseness_n<- closeness(full_no_zero_without_CAPS, mode = "out", normalized = T, weights = E(full_no_zero_without_CAPS)$full_no_zero) %>% round(6)
full_no_zero_without_CAPS_totalcloseness_n<- closeness(full_no_zero_without_CAPS, mode = "total", normalized = T, weights = E(full_no_zero_without_CAPS)$full_no_zero) %>% round(6)

23.5.3 Closeness Normalized - IN

summary(full_no_zero_without_CAPS_incloseness_n)
##     Min.  1st Qu.   Median     Mean  3rd Qu.     Max. 
## 0.005376 0.020300 0.020380 0.020240 0.020470 0.021390
sd(full_no_zero_without_CAPS_incloseness_n)
## [1] 0.001570409

23.6 Network Plotting Based On Normalized Closeness - IN

V(full_no_zero_without_CAPS)$full_no_zero_incloseness_n<-closeness(full_no_zero_without_CAPS, weights = E(full_no_zero_without_CAPS)$full_no_zero, mode="in", normalized = T)

#Get Variable
V(full_no_zero_without_CAPS)$full_no_zero_color_degree<-round(V(full_no_zero_without_CAPS)$full_no_zero_incloseness_n,6)

#Creating brewer pallette
full_no_zero_vertex_color_degree<-
  colorRampPalette(brewer.pal(length(unique(
          V(full_no_zero_without_CAPS)$full_no_zero_color_degree)), "RdBu"))(
            length(unique(V(full_no_zero_without_CAPS)$full_no_zero_color_degree)))

#Saving as Vertex properties 
V(full_no_zero_without_CAPS)$full_no_zero_vertex_color_degree<-
  full_no_zero_vertex_color_degree[as.numeric(
  cut(V(full_no_zero_without_CAPS)$full_no_zero_color_degree,
      breaks=length(unique(V(full_no_zero_without_CAPS)$full_no_zero_color_degree))))]

set.seed(123)
#Plotting based only on degree measures 
edge.start <- ends(full_no_zero_without_CAPS, es=E(full_no_zero_without_CAPS), names=F)[,1]

# Fixing ego
minC <- rep(-Inf, vcount(full_no_zero_without_CAPS))
maxC <- rep(Inf, vcount(full_no_zero_without_CAPS))
minC[1] <- maxC[1] <- 0
co <- layout_with_fr(full_no_zero_without_CAPS, niter=10^4, minx=minC, maxx=maxC,miny=minC, maxy=maxC, weights = E(full_no_zero_without_CAPS)$weight)


#PLotting
plot(full_no_zero_without_CAPS, 
     layout=co,
     edge.color=V(full_no_zero_without_CAPS)$full_no_zero_vertex_color_degree[edge.start],
     edge.arrow.size=closeness(full_no_zero_without_CAPS, weights = E(full_no_zero_without_CAPS)$full_no_zero, mode="in",normalized = T),
     edge.width=E(full_no_zero_without_CAPS)$weight/10*mean(E(full_no_zero_without_CAPS)$weight),
     edge.curved = TRUE,
     vertex.color=V(full_no_zero_without_CAPS)$full_no_zero_vertex_color_degree,
     vertex.size=(closeness(full_no_zero_without_CAPS, weights = E(full_no_zero_without_CAPS)$full_no_zero, mode="in",normalized = T))*1000,
     vertex.frame.color="black",
     vertex.label.color="black",
     vertex.label=get.vertex.attribute(full_no_zero_without_CAPS,"LABEL_COR"),
     vertex.label.cex=closeness(full_no_zero_without_CAPS, weights = E(full_no_zero_without_CAPS)$full_no_zero, mode="in",normalized = T)*10,
     vertex.label.dist=0,
     rescale=F,
     xlim=range(co[,1]), 
     ylim=range(co[,2])
     )
axis(1)
axis(2)


#Solving Problems with legend rendering 
a<-V(full_no_zero_without_CAPS)$full_no_zero_color_degree
b<-V(full_no_zero_without_CAPS)$full_no_zero_vertex_color_degree
c<-table(a,b)
d<-as.data.frame(c)
e<-subset(d, d$Freq>0)
e<-e[order(e$a,decreasing=T),] 
f<-t(e$a)
g<-t(e$b)

#Adding Legend
legend(x=range(co[,1])[2], 
       y=range(co[,2])[2],
       legend=as.character(f),
       pch=21,
       col = "#777777", 
       pt.bg=as.character(g),
       pt.cex=2,
       bty="n", 
       ncol=1,
       lty=1,
       cex = .3)

#Adding Title
  title("Network Closeness Degree Sized Normalized In - fuull_no_zero_fancy", sub = "Source: from authors ")
  text( 
    x=range(co[,1])[1],
    y=range(co[,2])[1], 
      labels = sprintf(
             "Median In Closennes:%.4f\nSD In Closennes: %.5f",
             median(closeness(full_no_zero_without_CAPS, mode="in", weights = E(full_no_zero_without_CAPS)$full_no_zero, normalized = T)), 
             sd(closeness(full_no_zero_without_CAPS, mode="in", weights = E(full_no_zero_without_CAPS)$full_no_zero, normalized = T))
             )
       )

###Closeness Normalized - OUT

summary(full_no_zero_without_CAPS_outcloseness_n)
##     Min.  1st Qu.   Median     Mean  3rd Qu.     Max. 
## 0.005376 0.046240 0.203900 0.161200 0.224800 0.270100
sd(full_no_zero_without_CAPS_outcloseness_n)
## [1] 0.09276749

23.7 Network Plotting Based On Normalized Closeness - OUT

V(full_no_zero_without_CAPS)$full_no_zero_outcloseness_n<-closeness(full_no_zero_without_CAPS, weights = E(full_no_zero_without_CAPS)$full_no_zero, mode="out", normalized = T)

#Get Variable
V(full_no_zero_without_CAPS)$full_no_zero_color_degree<-round(V(full_no_zero_without_CAPS)$full_no_zero_outcloseness_n,6)

#Creating brewer pallette
full_no_zero_vertex_color_degree<-
  colorRampPalette(brewer.pal(length(unique(
          V(full_no_zero_without_CAPS)$full_no_zero_color_degree)), "RdBu"))(
            length(unique(V(full_no_zero_without_CAPS)$full_no_zero_color_degree)))

#Saving as Vertex properties 
V(full_no_zero_without_CAPS)$full_no_zero_vertex_color_degree<-
  full_no_zero_vertex_color_degree[as.numeric(
  cut(V(full_no_zero_without_CAPS)$full_no_zero_color_degree,
      breaks=length(unique(V(full_no_zero_without_CAPS)$full_no_zero_color_degree))))]

set.seed(123)
#Plotting based only on degree measures 
edge.start <- ends(full_no_zero_without_CAPS, es=E(full_no_zero_without_CAPS), names=F)[,1]

# Fixing ego
minC <- rep(-Inf, vcount(full_no_zero_without_CAPS))
maxC <- rep(Inf, vcount(full_no_zero_without_CAPS))
minC[1] <- maxC[1] <- 0
co <- layout_with_fr(full_no_zero_without_CAPS, niter=10^4, minx=minC, maxx=maxC,miny=minC, maxy=maxC, weights = E(full_no_zero_without_CAPS)$weight)


#PLotting
plot(full_no_zero_without_CAPS, 
     layout=co,
     edge.color=V(full_no_zero_without_CAPS)$full_no_zero_vertex_color_degree[edge.start],
     edge.arrow.size=closeness(full_no_zero_without_CAPS, weights = E(full_no_zero_without_CAPS)$full_no_zero, mode="out",normalized = T),
     edge.width=E(full_no_zero_without_CAPS)$weight/10*mean(E(full_no_zero_without_CAPS)$weight),
     edge.curved = TRUE,
     vertex.color=V(full_no_zero_without_CAPS)$full_no_zero_vertex_color_degree,
     vertex.size=(closeness(full_no_zero_without_CAPS, weights = E(full_no_zero_without_CAPS)$full_no_zero, mode="out",normalized = T))*100,
     vertex.frame.color="black",
     vertex.label.color="black",
     vertex.label=get.vertex.attribute(full_no_zero_without_CAPS,"LABEL_COR"),
     vertex.label.cex=closeness(full_no_zero_without_CAPS, weights = E(full_no_zero_without_CAPS)$full_no_zero, mode="out",normalized = T)*1.5,
     vertex.label.dist=0,
     rescale=F,
     xlim=range(co[,1]), 
     ylim=range(co[,2])
     )
axis(1)
axis(2)


#Solving Problems with legend rendering 
a<-V(full_no_zero_without_CAPS)$full_no_zero_color_degree
b<-V(full_no_zero_without_CAPS)$full_no_zero_vertex_color_degree
c<-table(a,b)
d<-as.data.frame(c)
e<-subset(d, d$Freq>0)
e<-e[order(e$a,decreasing=T),] 
f<-t(e$a)
g<-t(e$b)

#Adding Legend
legend(x=range(co[,1])[2], 
       y=range(co[,2])[2],
       legend=as.character(f),
       pch=21,
       col = "#777777", 
       pt.bg=as.character(g),
       pt.cex=2,
       bty="n", 
       ncol=1,
       lty=1,
       cex = .3)

#Adding Title
  title("Network Closeness Degree Sized Normalized OUT - fuull_no_zero_fancy", sub = "Source: from authors ")
  text( 
    x=range(co[,1])[1],
    y=range(co[,2])[1], 
      labels = sprintf(
             "Median OUT Closennes:%.4f\nSD OUT Closennes: %.5f",
             median(closeness(full_no_zero_without_CAPS, mode="out", weights = E(full_no_zero_without_CAPS)$full_no_zero, normalized = T)), 
             sd(closeness(full_no_zero_without_CAPS, mode="out", weights = E(full_no_zero_without_CAPS)$full_no_zero, normalized = T))
             )
       )

23.7.1 Closeness Normalized - ALL

summary(full_no_zero_without_CAPS_totalcloseness_n)
##     Min.  1st Qu.   Median     Mean  3rd Qu.     Max. 
## 0.005376 0.283900 0.304500 0.299700 0.316600 0.412000
sd(full_no_zero_without_CAPS_totalcloseness_n)
## [1] 0.0357218

23.8 Network Plotting Based On Normalized Closeness - ALL

V(full_no_zero_without_CAPS)$full_no_zero_allcloseness_n<-closeness(full_no_zero_without_CAPS, weights = E(full_no_zero_without_CAPS)$full_no_zero, mode="all", normalized = T)

#Get Variable
V(full_no_zero_without_CAPS)$full_no_zero_color_degree<-round(V(full_no_zero_without_CAPS)$full_no_zero_allcloseness_n,6)

#Creating brewer pallette
full_no_zero_vertex_color_degree<-
  colorRampPalette(brewer.pal(length(unique(
          V(full_no_zero_without_CAPS)$full_no_zero_color_degree)), "RdBu"))(
            length(unique(V(full_no_zero_without_CAPS)$full_no_zero_color_degree)))

#Saving as Vertex properties 
V(full_no_zero_without_CAPS)$full_no_zero_vertex_color_degree<-
  full_no_zero_vertex_color_degree[as.numeric(
  cut(V(full_no_zero_without_CAPS)$full_no_zero_color_degree,
      breaks=length(unique(V(full_no_zero_without_CAPS)$full_no_zero_color_degree))))]

set.seed(123)
#Plotting based only on degree measures 
edge.start <- ends(full_no_zero_without_CAPS, es=E(full_no_zero_without_CAPS), names=F)[,1]

# Fixing ego
minC <- rep(-Inf, vcount(full_no_zero_without_CAPS))
maxC <- rep(Inf, vcount(full_no_zero_without_CAPS))
minC[1] <- maxC[1] <- 0
co <- layout_with_fr(full_no_zero_without_CAPS, niter=10^4, minx=minC, maxx=maxC,miny=minC, maxy=maxC, weights = E(full_no_zero_without_CAPS)$weight)


#PLotting
plot(full_no_zero_without_CAPS, 
     layout=co,
     edge.color=V(full_no_zero_without_CAPS)$full_no_zero_vertex_color_degree[edge.start],
     edge.arrow.size=closeness(full_no_zero_without_CAPS, weights = E(full_no_zero_without_CAPS)$full_no_zero, mode="all",normalized = T),
     edge.width=E(full_no_zero_without_CAPS)$weight/10*mean(E(full_no_zero_without_CAPS)$weight),
     edge.curved = TRUE,
     vertex.color=V(full_no_zero_without_CAPS)$full_no_zero_vertex_color_degree,
     vertex.size=(closeness(full_no_zero_without_CAPS, weights = E(full_no_zero_without_CAPS)$full_no_zero, mode="all",normalized = T))*100,
     vertex.frame.color="black",
     vertex.label.color="black",
     vertex.label=get.vertex.attribute(full_no_zero_without_CAPS,"LABEL_COR"),
     vertex.label.cex=closeness(full_no_zero_without_CAPS, weights = E(full_no_zero_without_CAPS)$full_no_zero, mode="all",normalized = T)*1.5,
     vertex.label.dist=0,
     rescale=F,
     xlim=range(co[,1]), 
     ylim=range(co[,2])
     )
axis(1)
axis(2)


#Solving Problems with legend rendering 
a<-V(full_no_zero_without_CAPS)$full_no_zero_color_degree
b<-V(full_no_zero_without_CAPS)$full_no_zero_vertex_color_degree
c<-table(a,b)
d<-as.data.frame(c)
e<-subset(d, d$Freq>0)
e<-e[order(e$a,decreasing=T),] 
f<-t(e$a)
g<-t(e$b)

#Adding Legend
legend(x=range(co[,1])[2], 
       y=range(co[,2])[2],
       legend=as.character(f),
       pch=21,
       col = "#777777", 
       pt.bg=as.character(g),
       pt.cex=2,
       bty="n", 
       ncol=1,
       lty=1,
       cex = .3)

#Adding Title
  title("Network Closeness Degree Sized Normalized ALL - fuull_no_zero_fancy", sub = "Source: from authors ")
  text( 
    x=range(co[,1])[1],
    y=range(co[,2])[1], 
      labels = sprintf(
             "Median ALL Closennes:%.4f\nSD ALL Closennes: %.5f",
             median(closeness(full_no_zero_without_CAPS, mode="all", weights = E(full_no_zero_without_CAPS)$full_no_zero, normalized = T)), 
             sd(closeness(full_no_zero_without_CAPS, mode="all", weights = E(full_no_zero_without_CAPS)$full_no_zero, normalized = T))
             )
       )

23.9 Closeness Normalized

23.9.1 Saving to Igraph object

V(full_no_zero_without_CAPS)$full_no_zero_incloseness_n <- closeness(full_no_zero_without_CAPS, weights = E(full_no_zero_without_CAPS)$full_no_zero, mode = "in", normalized = T) %>% round(6)
V(full_no_zero_without_CAPS)$full_no_zero_outcloseness_n <- closeness(full_no_zero_without_CAPS, weights = E(full_no_zero_without_CAPS)$full_no_zero, mode = "out", normalized = T) %>% round(6)
V(full_no_zero_without_CAPS)$full_no_zero_totalcloseness_n <- closeness(full_no_zero_without_CAPS, weights = E(full_no_zero_without_CAPS)$full_no_zero, mode = "total", normalized = T) %>% round(6)

23.10 Centralization Closseness

V(full_no_zero_without_CAPS)$full_no_zero_centr_closeness<- centralization.closeness(full_no_zero_without_CAPS)$res
full_no_zero_centr_closeness<- centralization.closeness(full_no_zero_without_CAPS)$res
full_no_zero_without_CAPS_centr_closeness_all<- centralization.closeness(full_no_zero_without_CAPS)

23.10.1 Centralization

full_no_zero_without_CAPS_centr_closeness_all$centralization
## [1] 0.1100775

23.10.2 Theoretical Max

full_no_zero_without_CAPS_centr_closeness_all$theoretical_max
## [1] 184.0054

23.11 Network Plotting Based On Centralization Closeness

V(full_no_zero_without_CAPS)$full_no_zero_centr_closeness<- centralization.closeness(full_no_zero_without_CAPS)$res

#Get Variable
V(full_no_zero_without_CAPS)$full_no_zero_color_degree<-round(V(full_no_zero_without_CAPS)$full_no_zero_centr_closeness,6)

#Creating brewer pallette
full_no_zero_vertex_color_degree<-
  colorRampPalette(brewer.pal(length(unique(
          V(full_no_zero_without_CAPS)$full_no_zero_color_degree)), "Spectral"))(
            length(unique(V(full_no_zero_without_CAPS)$full_no_zero_color_degree)))

#Saving as Vertex properties 
V(full_no_zero_without_CAPS)$full_no_zero_vertex_color_degree<-
  full_no_zero_vertex_color_degree[as.numeric(
  cut(V(full_no_zero_without_CAPS)$full_no_zero_color_degree,
      breaks=length(unique(V(full_no_zero_without_CAPS)$full_no_zero_color_degree))))]

set.seed(123)
#Plotting based only on degree measures 
edge.start <- ends(full_no_zero_without_CAPS, es=E(full_no_zero_without_CAPS), names=F)[,1]

# Fixing ego
minC <- rep(-Inf, vcount(full_no_zero_without_CAPS))
maxC <- rep(Inf, vcount(full_no_zero_without_CAPS))
minC[1] <- maxC[1] <- 0
co <- layout_with_fr(full_no_zero_without_CAPS, niter=10^4, minx=minC, maxx=maxC,miny=minC, maxy=maxC, weights = E(full_no_zero_without_CAPS)$weight)


#PLotting
plot(full_no_zero_without_CAPS, 
     layout=co,
     edge.color=V(full_no_zero_without_CAPS)$full_no_zero_vertex_color_degree[edge.start],
     edge.arrow.size=centralization.closeness(full_no_zero_without_CAPS)$res,
     edge.width=E(full_no_zero_without_CAPS)$weight/10*mean(E(full_no_zero_without_CAPS)$weight),
     edge.curved = TRUE,
     vertex.color=V(full_no_zero_without_CAPS)$full_no_zero_vertex_color_degree,
     vertex.size=centralization.closeness(full_no_zero_without_CAPS)$res*100,
     vertex.frame.color="black",
     vertex.label.color="black",
     vertex.label=get.vertex.attribute(full_no_zero_without_CAPS,"LABEL_COR"),
     vertex.label.cex=centralization.closeness(full_no_zero_without_CAPS)$res,
     vertex.label.dist=0,
     rescale=F,
     xlim=range(co[,1]), 
     ylim=range(co[,2])
     )
axis(1)
axis(2)


#Solving Problems with legend rendering 
a<-V(full_no_zero_without_CAPS)$full_no_zero_color_degree
b<-V(full_no_zero_without_CAPS)$full_no_zero_vertex_color_degree
c<-table(a,b)
d<-as.data.frame(c)
e<-subset(d, d$Freq>0)
e<-e[order(e$a,decreasing=T),] 
f<-t(e$a)
g<-t(e$b)

#Adding Legend
legend(x=range(co[,1])[2], 
       y=range(co[,2])[2],
       legend=as.character(f),
       pch=21,
       col = "#777777", 
       pt.bg=as.character(g),
       pt.cex=2,
       bty="n", 
       ncol=1,
       lty=1,
       cex = .3)

#Adding Title
  title("Network Centralization Closeness - fuull_no_zero_fancy", sub = "Source: from authors ")
  text( 
    x=range(co[,1])[1],
    y=range(co[,2])[1], 
      labels = sprintf(
             "Median Centralization Closeness:%.4f\nSD Centralization Closeness: %.5f",
             median(centralization.closeness(full_no_zero_without_CAPS)$res), 
             sd(centralization.closeness(full_no_zero_without_CAPS)$res)
             )
       )

24 Closeness Dinamic Table

24.1 Getting Closeness Measures

full_no_zero_without_CAPS_incloseness<- closeness(full_no_zero_without_CAPS, weights = E(full_no_zero_without_CAPS)$full_no_zero, mode = "in") %>% round(6)
full_no_zero_without_CAPS_outcloseness<- closeness(full_no_zero_without_CAPS, weights = E(full_no_zero_without_CAPS)$full_no_zero, mode = "out") %>% round(6)
full_no_zero_without_CAPS_totalcloseness<- closeness(full_no_zero_without_CAPS, weights = E(full_no_zero_without_CAPS)$full_no_zero, mode = "total") %>% round(6)
full_no_zero_without_CAPS_incloseness_n<- closeness(full_no_zero_without_CAPS,weights = E(full_no_zero_without_CAPS)$full_no_zero, mode = "in", normalized = T) %>% round(6)
full_no_zero_without_CAPS_outcloseness_n<- closeness(full_no_zero_without_CAPS,weights = E(full_no_zero_without_CAPS)$full_no_zero, mode = "out", normalized = T) %>% round(6)
full_no_zero_without_CAPS_totalcloseness_n<- closeness(full_no_zero_without_CAPS,weights = E(full_no_zero_without_CAPS)$full_no_zero, mode = "total", normalized = T) %>% round(6)
full_no_zero_centr_closeness <- centralization.closeness(full_no_zero_without_CAPS)$res %>% round(6)

24.2 Creating a datagrame of measures

full_no_zero_without_CAPS_df_closseness <- data.frame(
full_no_zero_without_CAPS_incloseness,
full_no_zero_without_CAPS_outcloseness,
full_no_zero_without_CAPS_totalcloseness,
full_no_zero_without_CAPS_incloseness_n,
full_no_zero_without_CAPS_outcloseness_n,
full_no_zero_without_CAPS_totalcloseness_n,
full_no_zero_centr_closeness) %>% round(6)

#Adding type
full_no_zero_without_CAPS_df_closseness <-cbind(full_no_zero_without_CAPS_df_closseness, V(full_no_zero_without_CAPS)$LABEL_COR)

#Adding names
names(full_no_zero_without_CAPS_df_closseness) <- c("In Closeness", "Out Closeness", "Total Closeness","In Closeness Normalized", "Out Closeness Normalized", "Total Closeness Normalized","Centralization Closeness","Type")

#Ordering Variables
full_no_zero_without_CAPS_df_closseness<-full_no_zero_without_CAPS_df_closseness[c("Type","In Closeness", "Out Closeness", "Total Closeness","In Closeness Normalized", "Out Closeness Normalized","Total Closeness Normalized", "Centralization Closeness")]

24.3 General tabel - DT

datatable(full_no_zero_without_CAPS_df_closseness, filter = 'top')

24.4 Aggregating data from previous table - mean

aggdata_mean <-aggregate(full_no_zero_without_CAPS_df_closseness, by=list(full_no_zero_without_CAPS_df_closseness$Type), FUN=mean, na.rm=TRUE)

names(aggdata_mean) <- c("Group","Type","In Closeness(M)", "Out Closeness(M)", "Total Closeness(M)","In Closeness Normalized(M)", "Out Closeness Normalized(M)", "Total Closeness Normalized(M)","Centralization Closeness(M)")
  
#Removing Type variable
aggdata_mean<-aggdata_mean[,-c(2)]

24.5 Aggregating data from previous table - median

aggdata_median <-aggregate(. ~ Type, full_no_zero_without_CAPS_df_closseness, function(x) c(median=median(x))) 


names(aggdata_median) <- c("Group","In Closeness(median)", "Out Closeness(median)", "Total Closeness(median)","In Closeness Normalized(median)", "Out Closeness Normalized(median)", "Total Closeness Normalized(median)", "Centralization Closeness(median)")

#Removing Type variable
#aggdata_median<-aggdata_median[,-c(2)]

#Merging mean and median
total_table <- merge(aggdata_mean,aggdata_median,by="Group")

#Rounding
Group<-total_table[,c(1)] #Keeping group
total_table<-total_table[,-c(1)] %>% round(6) #Rouding
total_table<-cbind(Group,total_table) #Binding toghter

#Organizing Variabels
total_table<-total_table[c("Group","In Closeness(M)", "In Closeness(median)", "Out Closeness(M)", "Out Closeness(median)", "Total Closeness(M)","Total Closeness(median)","In Closeness Normalized(M)", "In Closeness Normalized(median)", "Out Closeness Normalized(M)", "Out Closeness Normalized(median)", "Total Closeness Normalized(M)","Total Closeness Normalized(median)", "Centralization Closeness(M)","Centralization Closeness(median)")]

24.6 Plotting final table with round for Closseness

datatable(total_table, filter = 'top')

24.7 Creating a datagrame of measures (Natureza Governamental)

full_no_zero_without_CAPS_df_closseness <- data.frame(
full_no_zero_without_CAPS_incloseness,
full_no_zero_without_CAPS_outcloseness,
full_no_zero_without_CAPS_totalcloseness,
full_no_zero_without_CAPS_incloseness_n,
full_no_zero_without_CAPS_outcloseness_n,
full_no_zero_without_CAPS_totalcloseness_n,
full_no_zero_centr_closeness) %>% round(6)

#Adding type
full_no_zero_without_CAPS_df_closseness <-cbind(full_no_zero_without_CAPS_df_closseness, V(full_no_zero_without_CAPS)$TIPO1)

#Adding names
names(full_no_zero_without_CAPS_df_closseness) <- c("In Closeness", "Out Closeness", "Total Closeness","In Closeness Normalized", "Out Closeness Normalized", "Total Closeness Normalized","Centralization Closeness","Type")

#Ordering Variables
full_no_zero_without_CAPS_df_closseness<-full_no_zero_without_CAPS_df_closseness[c("Type","In Closeness", "Out Closeness", "Total Closeness","In Closeness Normalized", "Out Closeness Normalized","Total Closeness Normalized", "Centralization Closeness")]

24.8 General tabel - DT

datatable(full_no_zero_without_CAPS_df_closseness, filter = 'top')

24.9 Aggregating data from previous table - mean

aggdata_mean <-aggregate(full_no_zero_without_CAPS_df_closseness, by=list(full_no_zero_without_CAPS_df_closseness$Type), FUN=mean, na.rm=TRUE)

names(aggdata_mean) <- c("Group","Type","In Closeness(M)", "Out Closeness(M)", "Total Closeness(M)","In Closeness Normalized(M)", "Out Closeness Normalized(M)", "Total Closeness Normalized(M)","Centralization Closeness(M)")
  
#Removing Type variable
aggdata_mean<-aggdata_mean[,-c(2)]

24.10 Aggregating data from previous table - median

aggdata_median <-aggregate(. ~ Type, full_no_zero_without_CAPS_df_closseness, function(x) c(median=median(x))) 

names(aggdata_median) <- c("Group","In Closeness(median)", "Out Closeness(median)", "Total Closeness(median)","In Closeness Normalized(median)", "Out Closeness Normalized(median)", "Total Closeness Normalized(median)", "Centralization Closeness(median)")

#Removing Type variable
#aggdata_median<-aggdata_median[,-c(2)]

#Merging mean and median
total_table <- merge(aggdata_mean,aggdata_median,by="Group")

#Rounding
Group<-total_table[,c(1)] #Keeping group
total_table<-total_table[,-c(1)] %>% round(6) #Rouding
total_table<-cbind(Group,total_table) #Binding toghter

#Organizing Variabels
total_table<-total_table[c("Group","In Closeness(M)", "In Closeness(median)", "Out Closeness(M)", "Out Closeness(median)", "Total Closeness(M)","Total Closeness(median)","In Closeness Normalized(M)", "In Closeness Normalized(median)", "Out Closeness Normalized(M)", "Out Closeness Normalized(median)", "Total Closeness Normalized(M)","Total Closeness Normalized(median)", "Centralization Closeness(M)","Centralization Closeness(median)")]

24.11 Plotting final table with round for Closseness

datatable(total_table, filter = 'top')

24.12 Creating a datagrame of measures (Setores)

full_no_zero_without_CAPS_df_closseness <- data.frame(
full_no_zero_without_CAPS_incloseness,
full_no_zero_without_CAPS_outcloseness,
full_no_zero_without_CAPS_totalcloseness,
full_no_zero_without_CAPS_incloseness_n,
full_no_zero_without_CAPS_outcloseness_n,
full_no_zero_without_CAPS_totalcloseness_n,
full_no_zero_centr_closeness) %>% round(6)

#Adding type
full_no_zero_without_CAPS_df_closseness <-cbind(full_no_zero_without_CAPS_df_closseness, V(full_no_zero_without_CAPS)$TIPO2)

#Adding names
names(full_no_zero_without_CAPS_df_closseness) <- c("In Closeness", "Out Closeness", "Total Closeness","In Closeness Normalized", "Out Closeness Normalized", "Total Closeness Normalized","Centralization Closeness","Type")

#Ordering Variables
full_no_zero_without_CAPS_df_closseness<-full_no_zero_without_CAPS_df_closseness[c("Type","In Closeness", "Out Closeness", "Total Closeness","In Closeness Normalized", "Out Closeness Normalized","Total Closeness Normalized", "Centralization Closeness")]

24.13 General tabel - DT

datatable(full_no_zero_without_CAPS_df_closseness, filter = 'top')

24.14 Aggregating data from previous table - mean

aggdata_mean <-aggregate(full_no_zero_without_CAPS_df_closseness, by=list(full_no_zero_without_CAPS_df_closseness$Type), FUN=mean, na.rm=TRUE)

names(aggdata_mean) <- c("Group","Type","In Closeness(M)", "Out Closeness(M)", "Total Closeness(M)","In Closeness Normalized(M)", "Out Closeness Normalized(M)", "Total Closeness Normalized(M)","Centralization Closeness(M)")
  
#Removing Type variable
aggdata_mean<-aggdata_mean[,-c(2)]

24.15 Aggregating data from previous table - median

aggdata_median <-aggregate(. ~ Type, full_no_zero_without_CAPS_df_closseness, function(x) c(median=median(x))) 

names(aggdata_median) <- c("Group","In Closeness(median)", "Out Closeness(median)", "Total Closeness(median)","In Closeness Normalized(median)", "Out Closeness Normalized(median)", "Total Closeness Normalized(median)", "Centralization Closeness(median)")

#Removing Type variable
#aggdata_median<-aggdata_median[,-c(2)]

#Merging mean and median
total_table <- merge(aggdata_mean,aggdata_median,by="Group")

#Rounding
Group<-total_table[,c(1)] #Keeping group
total_table<-total_table[,-c(1)] %>% round(6) #Rouding
total_table<-cbind(Group,total_table) #Binding toghter

#Organizing Variabels
total_table<-total_table[c("Group","In Closeness(M)", "In Closeness(median)", "Out Closeness(M)", "Out Closeness(median)", "Total Closeness(M)","Total Closeness(median)","In Closeness Normalized(M)", "In Closeness Normalized(median)", "Out Closeness Normalized(M)", "Out Closeness Normalized(median)", "Total Closeness Normalized(M)","Total Closeness Normalized(median)", "Centralization Closeness(M)","Centralization Closeness(median)")]

24.16 Plotting final table with round for Closseness

datatable(total_table, filter = 'top')

25 Betweenness - Number of shortest paths going through the actor σst(i)

High betweenness centrality - vertex lies on many shortest paths. Probability that a communication from s to t will go through i - considering σst(i).

Betweenness measures the number of shortest paths between nodes in the network that go through the node in question. Nodes with relatively high betweenness are likely to be key conduits of information flow across a network, and their removal may have a large impact on spreading phenomena.

25.1 Betweenness Centrality -(Vertex)

Betweenness centrality based on a broker position connecting others or Number of geodesics that pass through the node or the edge. A higher number means an more important node.

26 Adding weight equal one for all analysis

E(full_no_zero_without_CAPS)$equalone<-1

26.0.1 Saving objects

V(full_no_zero_without_CAPS)$full_no_zero_betweenness <- betweenness(full_no_zero_without_CAPS, weights = E(full_no_zero_without_CAPS)$equalone) %>% round(6)
full_no_zero_without_CAPS_betweenness <- betweenness(full_no_zero_without_CAPS, weights = E(full_no_zero_without_CAPS)$equalone) %>% round(6)

#Z Score 
V(full_no_zero_without_CAPS)$full_no_zero_without_CAPS_betweenness_zscore <- (V(full_no_zero_without_CAPS)$full_no_zero_betweenness - mean(V(full_no_zero_without_CAPS)$full_no_zero_betweenness))/sd(V(full_no_zero_without_CAPS)$full_no_zero_betweenness)

#Normalized
V(full_no_zero_without_CAPS)$full_no_zero_without_CAPS_betweenness_norm <- (V(full_no_zero_without_CAPS)$full_no_zero_betweenness - min(V(full_no_zero_without_CAPS)$full_no_zero_betweenness))/max(V(full_no_zero_without_CAPS)$full_no_zero_betweenness)-min(V(full_no_zero_without_CAPS)$full_no_zero_betweenness)

26.0.2 Betweenness Centrality - all

summary(full_no_zero_without_CAPS_betweenness)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##    0.00    0.00   20.88  238.70  205.50 8866.00
sd(full_no_zero_without_CAPS_betweenness)
## [1] 778.2449

26.1 Betweenness Centrality Weighted - (Vertex)

V(full_no_zero_without_CAPS)$full_no_zero_betweenness_w <- betweenness(full_no_zero_without_CAPS, weights=E(full_no_zero_without_CAPS)$full_no_zero) %>% round(6)
full_no_zero_betweenness_w <- betweenness(full_no_zero_without_CAPS, weights=E(full_no_zero_without_CAPS)$full_no_zero) %>% round(6)

26.1.1 Descriptive Betweenness Centrality Weighted - (Vertex) - all

summary(full_no_zero_betweenness_w)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##    0.00    0.00   20.88  238.70  205.50 8866.00
sd(full_no_zero_betweenness_w)
## [1] 778.2449

26.2 Centralization Betweenness

V(full_no_zero_without_CAPS)$full_no_zero_centr_betweenness <- centralization.betweenness(full_no_zero_without_CAPS)$res
full_no_zero_centr_betweenness <- centralization.betweenness(full_no_zero_without_CAPS)

26.2.1 Centralization

full_no_zero_centr_betweenness$centralization
## [1] 0.2548288

26.2.2 Theoretical Max

full_no_zero_centr_betweenness$theoretical_max
## [1] 6297400

27 Betweenness Vertex Centrality Dinamic Table

#Betweenness Vertex Centrality Measures Dinamic Table
#Getting  Measures
full_no_zero_without_CAPS_betweenness <- betweenness(full_no_zero_without_CAPS, weights=E(full_no_zero_without_CAPS)$equalone) %>% round(6)
full_no_zero_betweenness_w <- betweenness(full_no_zero_without_CAPS, weights=E(full_no_zero_without_CAPS)$full_no_zero) %>% round(6)
full_no_zero_centr_betweenness <- centralization.betweenness(full_no_zero_without_CAPS)$res %>% round(6)

#Creating a dataframe of measures
full_no_zero_without_CAPS_df_betweenness <- data.frame(full_no_zero_without_CAPS_betweenness,
full_no_zero_betweenness_w,
full_no_zero_centr_betweenness) %>% round(6)

#Adding type
full_no_zero_without_CAPS_df_betweenness <-cbind(full_no_zero_without_CAPS_df_betweenness, V(full_no_zero_without_CAPS)$LABEL_COR)

#Adding names
names(full_no_zero_without_CAPS_df_betweenness) <- c("Betweenness", "Betweenness Weighted", "Centralization Betweenness","Type")

#Ordering Variables
full_no_zero_without_CAPS_df_betweenness<-full_no_zero_without_CAPS_df_betweenness[c("Type","Betweenness", "Betweenness Weighted", "Centralization Betweenness")]

27.1 General tabel for Betweenness

datatable(full_no_zero_without_CAPS_df_betweenness, filter = 'top')

##Aggregating data from previous table - mean

aggdata_mean <-aggregate(full_no_zero_without_CAPS_df_betweenness, by=list(full_no_zero_without_CAPS_df_betweenness$Type), FUN=mean, na.rm=TRUE)

names(aggdata_mean) <- c("Group","Type","Betweenness(M)", "Betweenness Weighted(M)", "Centralization Betweenness(M)")
  
#Removing Type variable
aggdata_mean<-aggdata_mean[,-c(2)]

27.2 Aggregating data from previous table - median

aggdata_median <-aggregate(. ~ Type, full_no_zero_without_CAPS_df_betweenness, function(x) c(median=median(x)))  

names(aggdata_median) <- c("Group","Betweenness(median)", "Betweenness Weighted(median)", "Centralization Betweenness(median)")

#Removing Type variable
#aggdata_median<-aggdata_median[,-c(2)]

27.3 Merging mean and median

total_table<- merge(aggdata_mean,aggdata_median,by="Group")

#Rounding
Group<-total_table[,c(1)] #Keeping group
total_table<-total_table[,-c(1)] %>% round(1) #Rouding
total_table<-cbind(Group,total_table) #Binding toghter

#Organizing Variabels
total_table<-total_table[c("Group","Betweenness(M)","Betweenness(median)","Betweenness Weighted(M)", "Betweenness Weighted(median)", "Centralization Betweenness(M)","Centralization Betweenness(median)")]

27.4 Plotting final table with round

datatable(total_table, filter = 'top')

28 Betweenness Vertex Centrality Dinamic Table (Natureza Governamental)

#Betweenness Vertex Centrality Measures Dinamic Table
#Getting  Measures
full_no_zero_without_CAPS_betweenness <- betweenness(full_no_zero_without_CAPS, weights=E(full_no_zero_without_CAPS)$equalone) %>% round(6)
full_no_zero_betweenness_w <- betweenness(full_no_zero_without_CAPS, weights=E(full_no_zero_without_CAPS)$full_no_zero) %>% round(6)
full_no_zero_centr_betweenness <- centralization.betweenness(full_no_zero_without_CAPS)$res %>% round(6)

#Creating a dataframe of measures
full_no_zero_without_CAPS_df_betweenness <- data.frame(full_no_zero_without_CAPS_betweenness,
full_no_zero_betweenness_w,
full_no_zero_centr_betweenness) %>% round(6)

#Adding type
full_no_zero_without_CAPS_df_betweenness <-cbind(full_no_zero_without_CAPS_df_betweenness, V(full_no_zero_without_CAPS)$TIPO1)

#Adding names
names(full_no_zero_without_CAPS_df_betweenness) <- c("Betweenness", "Betweenness Weighted", "Centralization Betweenness","Type")

#Ordering Variables
full_no_zero_without_CAPS_df_betweenness<-full_no_zero_without_CAPS_df_betweenness[c("Type","Betweenness", "Betweenness Weighted", "Centralization Betweenness")]

28.1 General tabel for Betweenness

datatable(full_no_zero_without_CAPS_df_betweenness, filter = 'top')

##Aggregating data from previous table - mean

aggdata_mean <-aggregate(full_no_zero_without_CAPS_df_betweenness, by=list(full_no_zero_without_CAPS_df_betweenness$Type), FUN=mean, na.rm=TRUE)

names(aggdata_mean) <- c("Group","Type","Betweenness(M)", "Betweenness Weighted(M)", "Centralization Betweenness(M)")
  
#Removing Type variable
aggdata_mean<-aggdata_mean[,-c(2)]

28.2 Aggregating data from previous table - median

aggdata_median <-aggregate(. ~ Type, full_no_zero_without_CAPS_df_betweenness, function(x) c(median=median(x)))  

names(aggdata_median) <- c("Group","Betweenness(median)", "Betweenness Weighted(median)", "Centralization Betweenness(median)")

#Removing Type variable
#aggdata_median<-aggdata_median[,-c(2)]

28.3 Merging mean and median

total_table<- merge(aggdata_mean,aggdata_median,by="Group")

#Rounding
Group<-total_table[,c(1)] #Keeping group
total_table<-total_table[,-c(1)] %>% round(6) #Rouding
total_table<-cbind(Group,total_table) #Binding toghter

#Organizing Variabels
total_table<-total_table[c("Group","Betweenness(M)","Betweenness(median)","Betweenness Weighted(M)", "Betweenness Weighted(median)", "Centralization Betweenness(M)","Centralization Betweenness(median)")]

28.4 Plotting final table with round

datatable(total_table, filter = 'top')

29 Betweenness Vertex Centrality Dinamic Table (Setores)

#Betweenness Vertex Centrality Measures Dinamic Table
#Getting  Measures
full_no_zero_without_CAPS_betweenness <- betweenness(full_no_zero_without_CAPS, weights=E(full_no_zero_without_CAPS)$equalone) %>% round(6)
full_no_zero_betweenness_w <- betweenness(full_no_zero_without_CAPS, weights=E(full_no_zero_without_CAPS)$full_no_zero) %>% round(1)
full_no_zero_centr_betweenness <- centralization.betweenness(full_no_zero_without_CAPS)$res %>% round(6)

#Creating a dataframe of measures
full_no_zero_without_CAPS_df_betweenness <- data.frame(full_no_zero_without_CAPS_betweenness,
full_no_zero_betweenness_w,
full_no_zero_centr_betweenness) %>% round(6)

#Adding type
full_no_zero_without_CAPS_df_betweenness <-cbind(full_no_zero_without_CAPS_df_betweenness, V(full_no_zero_without_CAPS)$TIPO2)

#Adding names
names(full_no_zero_without_CAPS_df_betweenness) <- c("Betweenness", "Betweenness Weighted", "Centralization Betweenness","Type")

#Ordering Variables
full_no_zero_without_CAPS_df_betweenness<-full_no_zero_without_CAPS_df_betweenness[c("Type","Betweenness", "Betweenness Weighted", "Centralization Betweenness")]

29.1 General tabel for Betweenness

datatable(full_no_zero_without_CAPS_df_betweenness, filter = 'top')

29.2 Aggregating data from previous table - mean

aggdata_mean <-aggregate(full_no_zero_without_CAPS_df_betweenness, by=list(full_no_zero_without_CAPS_df_betweenness$Type), FUN=mean, na.rm=TRUE)

names(aggdata_mean) <- c("Group","Type","Betweenness(M)", "Betweenness Weighted(M)", "Centralization Betweenness(M)")
  
#Removing Type variable
aggdata_mean<-aggdata_mean[,-c(2)]

29.3 Aggregating data from previous table - median

aggdata_median <-aggregate(. ~ Type, full_no_zero_without_CAPS_df_betweenness, function(x) c(median=median(x)))  

names(aggdata_median) <- c("Group","Betweenness(median)", "Betweenness Weighted(median)", "Centralization Betweenness(median)")

#Removing Type variable
#aggdata_median<-aggdata_median[,-c(2)]

29.4 Merging mean and median

total_table<- merge(aggdata_mean,aggdata_median,by="Group")

#Rounding
Group<-total_table[,c(1)] #Keeping group
total_table<-total_table[,-c(1)] %>% round(6) #Rouding
total_table<-cbind(Group,total_table) #Binding toghter

#Organizing Variabels
total_table<-total_table[c("Group","Betweenness(M)","Betweenness(median)","Betweenness Weighted(M)", "Betweenness Weighted(median)", "Centralization Betweenness(M)","Centralization Betweenness(median)")]

29.5 Plotting final table with round

datatable(total_table, filter = 'top')

29.6 Plotting Betweenness Centrality - (Vertex)

set.seed(123)
#Plotting based only on degree measures 
edge.start <- ends(full_no_zero_without_CAPS, es=E(full_no_zero_without_CAPS), names=F)[,1]

# Fixing ego
minC <- rep(-Inf, vcount(full_no_zero_without_CAPS))
maxC <- rep(Inf, vcount(full_no_zero_without_CAPS))
minC[1] <- maxC[1] <- 0
co <- layout_with_fr(full_no_zero_without_CAPS, niter=10^4, minx=minC, maxx=maxC,miny=minC, maxy=maxC, weights = E(full_no_zero_without_CAPS)$weight)

#Plotting
plot(full_no_zero_without_CAPS, 
     layout=co,
     edge.color=V(full_no_zero_without_CAPS)$color[edge.start],
     edge.arrow.size=(betweenness(full_no_zero_without_CAPS, weights = E(full_no_zero_without_CAPS)$full_no_zero)+1)/100000,
     edge.width=E(full_no_zero_without_CAPS)$weight/10*mean(E(full_no_zero_without_CAPS)$weight),
     edge.curved = TRUE,
     vertex.color=V(full_no_zero_without_CAPS)$color,
     vertex.size=betweenness(full_no_zero_without_CAPS, weights = E(full_no_zero_without_CAPS)$full_no_zero )/150,
     vertex.frame.color="black",
     vertex.label.color="black",
     vertex.label=get.vertex.attribute(full_no_zero_without_CAPS,"LABEL_COR"),
     vertex.label.cex=(betweenness(full_no_zero_without_CAPS, weights = E(full_no_zero_without_CAPS)$full_no_zero)+1)/10000,
     vertex.label.dist=0,
     rescale=F,
     xlim=range(co[,1]), 
     ylim=range(co[,2])
     )
axis(1)
axis(2)

#Solving Problems with legend rendering 
a<-V(full_no_zero_without_CAPS)$LABEL_COR
b<-V(full_no_zero_without_CAPS)$color
c<-table(a,b)
d<-as.data.frame(c)
e<-subset(d, d$Freq>0)
e<-e[order(e$a,decreasing=T),] 
f<-t(e$a)
g<-t(e$b)

#Adding Legend
legend(x=range(co[,1])[2], 
       y=range(co[,2])[2],
       legend=as.character(f),
       pch=21,
       col = "#777777", 
       pt.bg=as.character(g),
       pt.cex=2,
       bty="n", 
       ncol=1,
       lty=1,
       cex = .3)

#Adding Title
    title("Network Vertex Betweenness Sized - fuull_no_zero_fancy", sub = "Source: from authors ")
  text( 
    x=range(co[,1])[1],
    y=range(co[,2])[1], 
      labels =    sprintf("Median Betweenness: %.2f\nSD Betweenness: %.2f",
     median(betweenness(full_no_zero_without_CAPS, weights = E(full_no_zero_without_CAPS)$full_no_zero)), 
     sd(betweenness(full_no_zero_without_CAPS, weights = E(full_no_zero_without_CAPS)$full_no_zero))
             )
       )

29.7 Network Plotting Based On Centralization Betweenness

#Get Variable
V(full_no_zero_without_CAPS)$full_no_zero_centr_betweenness<-(centralization.betweenness(full_no_zero_without_CAPS)$res)/100
V(full_no_zero_without_CAPS)$full_no_zero_color_degree<-round(V(full_no_zero_without_CAPS)$full_no_zero_centr_betweenness,6)

#Creating brewer pallette
full_no_zero_vertex_color_degree<-
  colorRampPalette(brewer.pal(length(unique(
          V(full_no_zero_without_CAPS)$full_no_zero_color_degree)), "Spectral"))(
            length(unique(V(full_no_zero_without_CAPS)$full_no_zero_color_degree)))

#Saving as Vertex properties 
V(full_no_zero_without_CAPS)$full_no_zero_vertex_color_degree<-
  full_no_zero_vertex_color_degree[as.numeric(
  cut(V(full_no_zero_without_CAPS)$full_no_zero_color_degree,
      breaks=length(unique(V(full_no_zero_without_CAPS)$full_no_zero_color_degree))))]

set.seed(123)
#Plotting based only on degree measures 
edge.start <- ends(full_no_zero_without_CAPS, es=E(full_no_zero_without_CAPS), names=F)[,1]

# Fixing ego
minC <- rep(-Inf, vcount(full_no_zero_without_CAPS))
maxC <- rep(Inf, vcount(full_no_zero_without_CAPS))
minC[1] <- maxC[1] <- 0
co <- layout_with_fr(full_no_zero_without_CAPS, niter=10^4, minx=minC, maxx=maxC,miny=minC, maxy=maxC, weights = E(full_no_zero_without_CAPS)$weight)


#PLotting
plot(full_no_zero_without_CAPS, 
     layout=co,
     edge.color=V(full_no_zero_without_CAPS)$full_no_zero_vertex_color_degree[edge.start],
     edge.arrow.size=closeness(full_no_zero_without_CAPS, weights = E(full_no_zero_without_CAPS)$full_no_zero, mode="out"),
     edge.width=E(full_no_zero_without_CAPS)$weight/10*mean(E(full_no_zero_without_CAPS)$weight),
     edge.curved = TRUE,
     vertex.color=V(full_no_zero_without_CAPS)$full_no_zero_vertex_color_degree,
     vertex.size=(centralization.betweenness(full_no_zero_without_CAPS)$res+1)/50,
     vertex.frame.color="white",
     vertex.label.color="black",
     vertex.label=get.vertex.attribute(full_no_zero_without_CAPS,"LABEL_COR"),
     vertex.label.cex=(centralization.betweenness(full_no_zero_without_CAPS)$res + 1)/1000,
     vertex.label.dist=0,
     rescale=F,
     xlim=range(co[,1]), 
     ylim=range(co[,2])
     )
axis(1)
axis(2)

#Solving Problems with legend rendering 
a<-V(full_no_zero_without_CAPS)$full_no_zero_color_degree
b<-V(full_no_zero_without_CAPS)$full_no_zero_vertex_color_degree
c<-table(a,b)
d<-as.data.frame(c)
e<-subset(d, d$Freq>0)
e<-e[order(e$a,decreasing=T),] 
f<-t(e$a)
g<-t(e$b)

#Adding Legend
legend(x=range(co[,1])[2], 
       y=range(co[,2])[2],
       legend=as.character(f),
       pch=21,
       col = "#777777", 
       pt.bg=as.character(g),
       pt.cex=2,
       bty="n", 
       ncol=1,
       lty=1,
       cex = .3)

  #Adding Title
  title("Network Centralization Betweenness - fuull_no_zero_fancy", sub = "Source: from authors ")
  text( 
    x=range(co[,1])[1],
    y=range(co[,2])[1], 
      labels = sprintf("Median Betweenness:%.0f\nSDBetweenness: %.0f",
     median(betweenness(full_no_zero_without_CAPS, weights = E(full_no_zero_without_CAPS)$full_no_zero)), 
     sd(betweenness(full_no_zero_without_CAPS, weights = E(full_no_zero_without_CAPS)$full_no_zero))
             )
       )

30 Reciprocity Default

Reciprocity Default - Proportion of mutual connections - probability that hte opposite counterpart of a directed graph is also included

reciprocity(full_no_zero_without_CAPS, mode="default")
## [1] 0.3457875

31 Reciprocity Ratio

Reciprocity Ratio - Probability of mutual connections between a vertex pair - if we know - probability that hte opposite counterpart of a directed graph is also included in the

reciprocity(full_no_zero_without_CAPS, mode="ratio")
## [1] 0.2090345

32 Dyad Census

A dyad consists of an unordered pair of actors and links that exist between two actors of the pair classified by mutal non-mutual and no connection in a directed graphs

Dyads are 2-subgraphs where a subgraph is a subset of actors taken from the complete set of network actors and all links between them. See more here http://file.scirp.org/pdf/SN_2013012915270187.pdf

32.1 Dyad Census

Number of pairs with mutual connections “mut” and number of pairs with non-mutual connections “asym”

dyad.census_full_no_zero_without_CAPS<-dyad.census(full_no_zero_without_CAPS)

32.2 Mutual connections.

dyad.census_full_no_zero_without_CAPS_mut<-dyad.census_full_no_zero_without_CAPS$mut
dyad.census_full_no_zero_without_CAPS_mut
## [1] 238

32.3 Non-mutual connections.

dyad.census_full_no_zero_without_CAPS_asym<-dyad.census_full_no_zero_without_CAPS$asym
dyad.census_full_no_zero_without_CAPS_asym
## [1] 893

32.4 No connection between them.

dyad.census_full_no_zero_without_CAPS_null<-dyad.census_full_no_zero_without_CAPS$null
dyad.census_full_no_zero_without_CAPS_null
## [1] 16074

33 Triad Census - Check this out in order to understand triad lables

The studies about transitivity in social networks led Holland and Leinhardt (1975) to propose that the local structure in social networks can be expressed by the triad census or triad count, the numbers of triads of any kinds.

You can see more here: http://www.stats.ox.ac.uk/~snijders/Trans_Triads_ha.pdf

#Triad Census 
tc_full_no_zero_without_CAPS <- triad.census(full_no_zero_without_CAPS)

#Triad Census Label 
census_labels = c('T.003',
                  'T.012',
                  'T.102',
                  'T.021D',
                  'T.021U',
                  'T.021C',
                  'T.111D',
                  'T.111U',
                  'T.030T',
                  'T.030C',
                  'T.201',
                  'T.120D',
                  'T.120U',
                  'T.120C',
                  'T.210',
                  'T.300')

ordering = c('1',
                  '2',
                  '3',
                  '4',
                  '5',
                  '6',
                  '7',
                  '8',
                  '13',
                  '10',
                  '9',
                  '14',
                  '15',
                  '11',
                  '12',
                  '16')

#Saving in a dataframe for further studies
triad_df_full_no_zero_without_CAPS <- data.frame(census_labels,tc_full_no_zero_without_CAPS)
write.csv(triad_df_full_no_zero_without_CAPS, "~/SNArRDJF/Banco Redes R/full_no_zero_without_CAPS_complet_triads.csv")

33.1 Triad Census Types

The following labels gives the 16 different triads for directed graphs. The coding refers to the numbers of mutual, asymmetric, and null dyads, with a further identifying letter: Up, Down, Cyclical, Transitive.

E.g., 1-2-0-D has 1 mutual, 2 asymmetric, 0 null dyads, and the Down orientation.

33.1.1 Describing triads

triad_df_full_no_zero_without_CAPS
##    census_labels tc_full_no_zero_without_CAPS
## 1          T.003                       876907
## 2          T.012                       121018
## 3          T.102                        30526
## 4         T.021D                         7194
## 5         T.021U                         2628
## 6         T.021C                         4985
## 7         T.111D                         2759
## 8         T.111U                         5224
## 9         T.030T                          963
## 10        T.030C                           80
## 11         T.201                         1385
## 12        T.120D                          291
## 13        T.120U                          473
## 14        T.120C                          329
## 15         T.210                          382
## 16         T.300                           96

33.1.2 Triads Tables Recoding

#Recoding different types of triads 
triad_df_full_no_zero_without_CAPS$type[triad_df_full_no_zero_without_CAPS$census_labels=="T.003"]<-"Vacuously Transitive"
triad_df_full_no_zero_without_CAPS$type[triad_df_full_no_zero_without_CAPS$census_labels=="T.012"]<-"Vacuously Transitive"
triad_df_full_no_zero_without_CAPS$type[triad_df_full_no_zero_without_CAPS$census_labels=="T.102"]<-"Vacuously Transitive"
triad_df_full_no_zero_without_CAPS$type[triad_df_full_no_zero_without_CAPS$census_labels=="T.021D"]<-"Vacuously Transitive"
triad_df_full_no_zero_without_CAPS$type[triad_df_full_no_zero_without_CAPS$census_labels=="T.021U"]<-"Vacuously Transitive"
triad_df_full_no_zero_without_CAPS$type[triad_df_full_no_zero_without_CAPS$census_labels=="T.021C"]<-"Intransitive"
triad_df_full_no_zero_without_CAPS$type[triad_df_full_no_zero_without_CAPS$census_labels=="T.111D"]<-"Intransitive"
triad_df_full_no_zero_without_CAPS$type[triad_df_full_no_zero_without_CAPS$census_labels=="T.111U"]<-"Intransitive"
triad_df_full_no_zero_without_CAPS$type[triad_df_full_no_zero_without_CAPS$census_labels=="T.030T"]<-"Transitive"
triad_df_full_no_zero_without_CAPS$type[triad_df_full_no_zero_without_CAPS$census_labels=="T.030C"]<-"Intransitive"
triad_df_full_no_zero_without_CAPS$type[triad_df_full_no_zero_without_CAPS$census_labels=="T.201"]<-"Intransitive"
triad_df_full_no_zero_without_CAPS$type[triad_df_full_no_zero_without_CAPS$census_labels=="T.120D"]<-"Transitive"
triad_df_full_no_zero_without_CAPS$type[triad_df_full_no_zero_without_CAPS$census_labels=="T.120U"]<-"Transitive"
triad_df_full_no_zero_without_CAPS$type[triad_df_full_no_zero_without_CAPS$census_labels=="T.120C"]<-"Intransitive"
triad_df_full_no_zero_without_CAPS$type[triad_df_full_no_zero_without_CAPS$census_labels=="T.210"]<-"Intransitive"
triad_df_full_no_zero_without_CAPS$type[triad_df_full_no_zero_without_CAPS$census_labels=="T.300"]<-"Transitive"

Graphical Ilustration

33.1.3 Triads Tables

datatable(triad_df_full_no_zero_without_CAPS)

34 Transitivity - clustering coefficient

34.1 Transitivity Global

Socialnetwork is treated as an undirected network global - ratio of triangles (direction disregarded) to connected triples.

transitivity(full_no_zero_without_CAPS, type="global")
## [1] 0.2449324

35 Transitivity Local

Ratio of triangles to connected triples each vertex is part of.

V(full_no_zero_without_CAPS)$full_no_zero_transitivity_local<-transitivity(full_no_zero_without_CAPS, type="local") 
full_no_zero_without_CAPS_transitivity_local<-transitivity(full_no_zero_without_CAPS, type="local") #local - ratio of triangles to connected triples each vertex is part of.

36 Descriptive Statistics for Local Transitivity by Vertex

summary(full_no_zero_without_CAPS_transitivity_local[which(full_no_zero_without_CAPS_transitivity_local != Inf)])
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##  0.0000  0.2000  0.3003  0.3493  0.4367  1.0000
sd(full_no_zero_without_CAPS_transitivity_local[which(full_no_zero_without_CAPS_transitivity_local != Inf)])
## [1] 0.2196553

37 Barrat’s Weighted Transitivity by Edges (Barrat’s)

V(full_no_zero_without_CAPS)$full_no_zero_transitivity_barrat<-transitivity(full_no_zero_without_CAPS, weights=E(full_no_zero_without_CAPS)$weight, type="barrat")

full_no_zero_without_CAPS_transitivity_barrat<-transitivity(full_no_zero_without_CAPS, weights=E(full_no_zero_without_CAPS)$weight, type="barrat")

38 Descriptive Statistics for Barrat Weighted Transitivity by Vertex

summary(full_no_zero_without_CAPS_transitivity_barrat[which(full_no_zero_without_CAPS_transitivity_barrat != Inf)])
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##  0.0000  0.3550  0.5000  0.5519  0.7034  1.6670
sd(full_no_zero_without_CAPS_transitivity_barrat[which(full_no_zero_without_CAPS_transitivity_barrat != Inf)])
## [1] 0.3166695

39 Transitivity Measures Dinamic Table

#Getting  Measures

full_no_zero_without_CAPS_transitivity_local<-transitivity(full_no_zero_without_CAPS, type="local") %>% round(3)

full_no_zero_without_CAPS_transitivity_barrat<-transitivity(full_no_zero_without_CAPS, weights=E(full_no_zero_without_CAPS)$weight, type="barrat") %>% round(3)

#Creating a datagrame of measures
full_no_zero_without_CAPS_transitivity_df <- data.frame(full_no_zero_without_CAPS_transitivity_local,full_no_zero_without_CAPS_transitivity_barrat) %>% round(3)

40 Transitivity Measures Dinamic Table

full_no_zero_without_CAPS_transitivity_df <-cbind(full_no_zero_without_CAPS_transitivity_df, V(full_no_zero_without_CAPS)$LABEL_COR)

#Adding names
names(full_no_zero_without_CAPS_transitivity_df) <- c("Local", "Barrat's Weighted","Type")

#Ordering Variables
full_no_zero_without_CAPS_transitivity_df<-full_no_zero_without_CAPS_transitivity_df[c("Type", "Local", "Barrat's Weighted")]

40.1 General tabel - DT

datatable(full_no_zero_without_CAPS_transitivity_df, filter = 'top')

40.2 Aggregating data from previous table - mean

aggdata_mean <-aggregate(full_no_zero_without_CAPS_transitivity_df, by=list(full_no_zero_without_CAPS_transitivity_df$Type), FUN=mean, na.rm=TRUE)

names(aggdata_mean) <- c("Group","Type","Local(M)", "Barrat's Weighted(M)")
  
#Removing Type variable
aggdata_mean<-aggdata_mean[,-c(2)]

40.3 Aggregating data from previous table - median

aggdata_median <-aggregate(. ~ Type, full_no_zero_without_CAPS_transitivity_df, function(x) c(median=median(x))) 

names(aggdata_median) <- c("Group","Local(median)", "Barrat's Weighted(median)")

#Removing Type variable
#aggdata_median<-aggdata_median[,-c(2)]

40.4 Merging mean and median

total_table <- merge(aggdata_mean,aggdata_median,by="Group")

#Rounding
Group<-total_table[,c(1)] #Keeping group
total_table<-total_table[,-c(1)] %>% round(3) #Rouding
total_table<-cbind(Group,total_table) #Binding toghter

#Organizing Variabels
total_table<-total_table[c("Group","Local(M)","Local(median)", "Barrat's Weighted(M)","Barrat's Weighted(median)")]

40.5 Final table with round - Transitivity

datatable(total_table, filter = 'top')

41 Transitivity Measures Dinamic Table

#Getting  Measures

full_no_zero_without_CAPS_transitivity_local<-transitivity(full_no_zero_without_CAPS, type="local") %>% round(3)

full_no_zero_without_CAPS_transitivity_barrat<-transitivity(full_no_zero_without_CAPS, weights=E(full_no_zero_without_CAPS)$weight, type="barrat") %>% round(3)

#Creating a datagrame of measures
full_no_zero_without_CAPS_transitivity_df <- data.frame(full_no_zero_without_CAPS_transitivity_local,full_no_zero_without_CAPS_transitivity_barrat) %>% round(3)

42 Transitivity Measures Dinamic Table

full_no_zero_without_CAPS_transitivity_df <-cbind(full_no_zero_without_CAPS_transitivity_df, V(full_no_zero_without_CAPS)$TIPO1)

#Adding names
names(full_no_zero_without_CAPS_transitivity_df) <- c("Local", "Barrat's Weighted","Type")

#Ordering Variables
full_no_zero_without_CAPS_transitivity_df<-full_no_zero_without_CAPS_transitivity_df[c("Type", "Local", "Barrat's Weighted")]

42.1 General tabel - DT

datatable(full_no_zero_without_CAPS_transitivity_df, filter = 'top')

42.2 Aggregating data from previous table - mean

aggdata_mean <-aggregate(full_no_zero_without_CAPS_transitivity_df, by=list(full_no_zero_without_CAPS_transitivity_df$Type), FUN=mean, na.rm=TRUE)

names(aggdata_mean) <- c("Group","Type","Local(M)", "Barrat's Weighted(M)")
  
#Removing Type variable
aggdata_mean<-aggdata_mean[,-c(2)]

42.3 Aggregating data from previous table - median

aggdata_median <-aggregate(. ~ Type, full_no_zero_without_CAPS_transitivity_df, function(x) c(median=median(x))) 

names(aggdata_median) <- c("Group","Local(median)", "Barrat's Weighted(median)")

#Removing Type variable
#aggdata_median<-aggdata_median[,-c(2)]

42.4 Merging mean and median

total_table <- merge(aggdata_mean,aggdata_median,by="Group")

#Rounding
Group<-total_table[,c(1)] #Keeping group
total_table<-total_table[,-c(1)] %>% round(3) #Rouding
total_table<-cbind(Group,total_table) #Binding toghter

#Organizing Variabels
total_table<-total_table[c("Group","Local(M)","Local(median)", "Barrat's Weighted(M)","Barrat's Weighted(median)")]

42.5 Final table with round - Transitivity

datatable(total_table, filter = 'top')

43 Transitivity Measures Dinamic Table

#Getting  Measures

full_no_zero_without_CAPS_transitivity_local<-transitivity(full_no_zero_without_CAPS, type="local") %>% round(3)

full_no_zero_without_CAPS_transitivity_barrat<-transitivity(full_no_zero_without_CAPS, weights=E(full_no_zero_without_CAPS)$weight, type="barrat") %>% round(3)

#Creating a datagrame of measures
full_no_zero_without_CAPS_transitivity_df <- data.frame(full_no_zero_without_CAPS_transitivity_local,full_no_zero_without_CAPS_transitivity_barrat) %>% round(3)

44 Transitivity Measures Dinamic Table

full_no_zero_without_CAPS_transitivity_df <-cbind(full_no_zero_without_CAPS_transitivity_df, V(full_no_zero_without_CAPS)$TIPO2)

#Adding names
names(full_no_zero_without_CAPS_transitivity_df) <- c("Local", "Barrat's Weighted","Type")

#Ordering Variables
full_no_zero_without_CAPS_transitivity_df<-full_no_zero_without_CAPS_transitivity_df[c("Type", "Local", "Barrat's Weighted")]

44.1 General tabel - DT

datatable(full_no_zero_without_CAPS_transitivity_df, filter = 'top')

44.2 Aggregating data from previous table - mean

aggdata_mean <-aggregate(full_no_zero_without_CAPS_transitivity_df, by=list(full_no_zero_without_CAPS_transitivity_df$Type), FUN=mean, na.rm=TRUE)

names(aggdata_mean) <- c("Group","Type","Local(M)", "Barrat's Weighted(M)")
  
#Removing Type variable
aggdata_mean<-aggdata_mean[,-c(2)]

44.3 Aggregating data from previous table - median

aggdata_median <-aggregate(. ~ Type, full_no_zero_without_CAPS_transitivity_df, function(x) c(median=median(x))) 

names(aggdata_median) <- c("Group","Local(median)", "Barrat's Weighted(median)")

#Removing Type variable
#aggdata_median<-aggdata_median[,-c(2)]

44.4 Merging mean and median

total_table <- merge(aggdata_mean,aggdata_median,by="Group")

#Rounding
Group<-total_table[,c(1)] #Keeping group
total_table<-total_table[,-c(1)] %>% round(3) #Rouding
total_table<-cbind(Group,total_table) #Binding toghter

#Organizing Variabels
total_table<-total_table[c("Group","Local(M)","Local(median)", "Barrat's Weighted(M)","Barrat's Weighted(median)")]

44.5 Final table with round - Transitivity

datatable(total_table, filter = 'top')

45 Distances and paths

Defined as the shortest distance between each pair of nodes in the network (in both directions for directed graphs).

45.1 Average path length between any two given nodes

Calculates the average path length in a graph, by calculating the shortest paths between all pairs of vertices (both ways for directed graphs).

This function does not consider edge weights currently and uses a breadth-first search.

mean_distance(full_no_zero_without_CAPS, directed=T, unconnected = T)
## [1] 2.745274

45.2 Shortest Paths

#Shortest Paths
full_no_zero_without_CAPS_sp_in <- shortest.paths(full_no_zero_without_CAPS, mode='in', weights=E(full_no_zero_without_CAPS)$full_no_zero) #in

full_no_zero_without_CAPS_sp_out <- shortest.paths(full_no_zero_without_CAPS, mode='out', weights=E(full_no_zero_without_CAPS)$full_no_zero) # out

full_no_zero_without_CAPS_sp_all <- shortest.paths(full_no_zero_without_CAPS, mode='all', weights=E(full_no_zero_without_CAPS)$full_no_zero) # all

45.3 Descriptive Shortest Paths - IN

summary(full_no_zero_without_CAPS_sp_in[which(full_no_zero_without_CAPS_sp_in != Inf)])
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   0.000   2.000   3.000   2.725   3.000   6.000
sd(full_no_zero_without_CAPS_sp_in[which(full_no_zero_without_CAPS_sp_in != Inf)])
## [1] 0.9499275

45.4 Descriptive Shortest Paths - OUT

summary(full_no_zero_without_CAPS_sp_out[which(full_no_zero_without_CAPS_sp_out != Inf)])
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   0.000   2.000   3.000   2.725   3.000   6.000
sd(full_no_zero_without_CAPS_sp_out[which(full_no_zero_without_CAPS_sp_out != Inf)])
## [1] 0.9499275

45.5 Descriptive Shortest Paths - ALL

summary(full_no_zero_without_CAPS_sp_all[which(full_no_zero_without_CAPS_sp_all != Inf)])
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   0.000   2.000   2.000   2.345   3.000   5.000
sd(full_no_zero_without_CAPS_sp_all[which(full_no_zero_without_CAPS_sp_all != Inf)])
## [1] 0.7119887

46 Length of all shortest paths in the graph:

#All shortest paths 
distances_dist_all_full_no_zero_without_CAPS<-distances(full_no_zero_without_CAPS, mode="all", weights=E(full_no_zero_without_CAPS)$full_no_zero)
#distances_sp_all_full_no_zero_without_CAPS

distances_dist_all_full_no_zero_without_CAPS[distances_dist_all_full_no_zero_without_CAPS=="Inf"]<-NA

#Mean Reachbility by Vertex
distances_sp_all_full_no_zero_without_CAPS_vec <- vector()
for (i in 1:vcount(full_no_zero_without_CAPS)) {
    distances_sp_all_full_no_zero_without_CAPS_vec[i] <- 
    mean(distances_dist_all_full_no_zero_without_CAPS[i,],na.rm=T)
}
#Adding to igraph object
V(full_no_zero_without_CAPS)$full_no_zero_sp_all<-distances_sp_all_full_no_zero_without_CAPS_vec

47 In shortest paths

distances_dist_in_full_no_zero_without_CAPS<-distances(full_no_zero_without_CAPS, mode="in",weights=E(full_no_zero_without_CAPS)$full_no_zero)
#distances_sp_in_full_no_zero_without_CAPS

distances_dist_in_full_no_zero_without_CAPS[distances_dist_in_full_no_zero_without_CAPS=="Inf"]<-NA

#Mean Reachbility by Vertex
distances_sp_in_full_no_zero_without_CAPS_vec <- vector()
for (i in 1:vcount(full_no_zero_without_CAPS)) {
    distances_sp_in_full_no_zero_without_CAPS_vec[i] <- mean(distances_dist_in_full_no_zero_without_CAPS[i,], na.rm=T)
}

#Adding to igraph object
V(full_no_zero_without_CAPS)$full_no_zero_sp_in<-distances_sp_in_full_no_zero_without_CAPS_vec

48 Out shortest paths

distances_dist_out_full_no_zero_without_CAPS<-distances(full_no_zero_without_CAPS, mode="out", weights=E(full_no_zero_without_CAPS)$full_no_zero)

distances_dist_out_full_no_zero_without_CAPS[distances_dist_out_full_no_zero_without_CAPS=="Inf"]<-NA

#Mean Reachbility by Vertex
distances_sp_out_full_no_zero_without_CAPS_vec <- vector()
for (i in 1:vcount(full_no_zero_without_CAPS)) {
    distances_sp_out_full_no_zero_without_CAPS_vec[i] <- 
    mean(distances_dist_out_full_no_zero_without_CAPS[i,], na.rm = T)
}

#Adding to igraph object
V(full_no_zero_without_CAPS)$full_no_zero_sp_out<-distances_sp_out_full_no_zero_without_CAPS_vec

49 Reachbility Measures Dinamic Table

#Creating a datagrame of measures
full_no_zero_without_CAPS_shortpath_df <- data.frame(distances_sp_in_full_no_zero_without_CAPS_vec, distances_sp_out_full_no_zero_without_CAPS_vec, distances_sp_all_full_no_zero_without_CAPS_vec) %>% round(3)

#Adding type
full_no_zero_without_CAPS_shortpath_df <-cbind(full_no_zero_without_CAPS_shortpath_df, V(full_no_zero_without_CAPS)$LABEL_COR)

#Adding names
names(full_no_zero_without_CAPS_shortpath_df) <- c("Short Path IN", "Short Path OUT","Short Path ALL","Type") 

#Ordering Variables
full_no_zero_without_CAPS_shortpath_df<-full_no_zero_without_CAPS_shortpath_df[c("Type", "Short Path IN", "Short Path OUT","Short Path ALL")]

49.1 General tabel - DT

datatable(full_no_zero_without_CAPS_shortpath_df, filter = 'top')

49.2 Aggregating data from previous table - mean

aggdata_mean <-aggregate(full_no_zero_without_CAPS_shortpath_df, by=list(full_no_zero_without_CAPS_shortpath_df$Type), FUN=mean, na.rm=TRUE)

names(aggdata_mean) <- c("Group","Type","Short Path IN(M)", "Short Path OUT(M)","Short Path ALL(M)")
  
#Removing Type variable
aggdata_mean<-aggdata_mean[,-c(2)]

49.3 Aggregating data from previous table - median

aggdata_median <-aggregate(. ~ Type, full_no_zero_without_CAPS_shortpath_df, function(x) c(median=median(x)))

names(aggdata_median) <- c("Group", "Short Path IN(median)", "Short Path OUT(median)","Short Path ALL(median)")

#Removing Type variable
#aggdata_median<-aggdata_median[,-c(2)]

49.4 Merging mean and median

total_table <- merge(aggdata_mean,aggdata_median,by="Group")

#Rounding
Group<-total_table[,c(1)] #Keeping group
total_table<-total_table[,-c(1)] %>% round(3) #Rouding
total_table<-cbind(Group,total_table) #Binding toghter

#Organizing Variabels
total_table<-total_table[,c("Group","Short Path IN(M)","Short Path IN(median)","Short Path OUT(M)","Short Path OUT(median)","Short Path ALL(M)","Short Path ALL(median)")]

49.5 Final table with round - Short Path

datatable(total_table, filter = 'top')

50 Reachbility Measures Dinamic Table

#Creating a datagrame of measures
full_no_zero_without_CAPS_shortpath_df <- data.frame(distances_sp_in_full_no_zero_without_CAPS_vec, distances_sp_out_full_no_zero_without_CAPS_vec, distances_sp_all_full_no_zero_without_CAPS_vec) %>% round(3)

#Adding type
full_no_zero_without_CAPS_shortpath_df <-cbind(full_no_zero_without_CAPS_shortpath_df, V(full_no_zero_without_CAPS)$TIPO1)

#Adding names
names(full_no_zero_without_CAPS_shortpath_df) <- c("Short Path IN", "Short Path OUT","Short Path ALL","Type") 

#Ordering Variables
full_no_zero_without_CAPS_shortpath_df<-full_no_zero_without_CAPS_shortpath_df[c("Type", "Short Path IN", "Short Path OUT","Short Path ALL")]

50.1 General tabel - DT

datatable(full_no_zero_without_CAPS_shortpath_df, filter = 'top')

50.2 Aggregating data from previous table - mean

aggdata_mean <-aggregate(full_no_zero_without_CAPS_shortpath_df, by=list(full_no_zero_without_CAPS_shortpath_df$Type), FUN=mean, na.rm=TRUE)

names(aggdata_mean) <- c("Group","Type","Short Path IN(M)", "Short Path OUT(M)","Short Path ALL(M)")
  
#Removing Type variable
aggdata_mean<-aggdata_mean[,-c(2)]

50.3 Aggregating data from previous table - median

aggdata_median <-aggregate(. ~ Type, full_no_zero_without_CAPS_shortpath_df, function(x) c(median=median(x)))

names(aggdata_median) <- c("Group", "Short Path IN(median)", "Short Path OUT(median)","Short Path ALL(median)")

#Removing Type variable
#aggdata_median<-aggdata_median[,-c(2)]

50.4 Merging mean and median

total_table <- merge(aggdata_mean,aggdata_median,by="Group")

#Rounding
Group<-total_table[,c(1)] #Keeping group
total_table<-total_table[,-c(1)] %>% round(3) #Rouding
total_table<-cbind(Group,total_table) #Binding toghter

#Organizing Variabels
total_table<-total_table[,c("Group","Short Path IN(M)","Short Path IN(median)","Short Path OUT(M)","Short Path OUT(median)","Short Path ALL(M)","Short Path ALL(median)")]

50.5 Final table with round - Short Path

datatable(total_table, filter = 'top')

51 Reachbility Measures Dinamic Table

#Creating a datagrame of measures
full_no_zero_without_CAPS_shortpath_df <- data.frame(distances_sp_in_full_no_zero_without_CAPS_vec, distances_sp_out_full_no_zero_without_CAPS_vec, distances_sp_all_full_no_zero_without_CAPS_vec) %>% round(3)

#Adding type
full_no_zero_without_CAPS_shortpath_df <-cbind(full_no_zero_without_CAPS_shortpath_df, V(full_no_zero_without_CAPS)$TIPO2)

#Adding names
names(full_no_zero_without_CAPS_shortpath_df) <- c("Short Path IN", "Short Path OUT","Short Path ALL","Type") 

#Ordering Variables
full_no_zero_without_CAPS_shortpath_df<-full_no_zero_without_CAPS_shortpath_df[c("Type", "Short Path IN", "Short Path OUT","Short Path ALL")]

51.1 General tabel - DT

datatable(full_no_zero_without_CAPS_shortpath_df, filter = 'top')

51.2 Aggregating data from previous table - mean

aggdata_mean <-aggregate(full_no_zero_without_CAPS_shortpath_df, by=list(full_no_zero_without_CAPS_shortpath_df$Type), FUN=mean, na.rm=TRUE)

names(aggdata_mean) <- c("Group","Type","Short Path IN(M)", "Short Path OUT(M)","Short Path ALL(M)")
  
#Removing Type variable
aggdata_mean<-aggdata_mean[,-c(2)]

51.3 Aggregating data from previous table - median

aggdata_median <-aggregate(. ~ Type, full_no_zero_without_CAPS_shortpath_df, function(x) c(median=median(x)))

names(aggdata_median) <- c("Group", "Short Path IN(median)", "Short Path OUT(median)","Short Path ALL(median)")

#Removing Type variable
#aggdata_median<-aggdata_median[,-c(2)]

51.4 Merging mean and median

total_table <- merge(aggdata_mean,aggdata_median,by="Group")

#Rounding
Group<-total_table[,c(1)] #Keeping group
total_table<-total_table[,-c(1)] %>% round(3) #Rouding
total_table<-cbind(Group,total_table) #Binding toghter

#Organizing Variabels
total_table<-total_table[,c("Group","Short Path IN(M)","Short Path IN(median)","Short Path OUT(M)","Short Path OUT(median)","Short Path ALL(M)","Short Path ALL(median)")]

51.5 Final table with round - Short Path

datatable(total_table, filter = 'top')

51.6 Histogram from shortest path length between each pair of vertices.

sp<-distance_table(full_no_zero_without_CAPS, directed = TRUE)
short_paths<-c(sp$unconnected, sp$res)
labels<-c("unconnected", "one", "two","three", "four", "five", "six")
sphist<-as.data.frame(cbind(labels, short_paths))
names(sphist)<-c("Short Paths Length - Vertex Pairs","Count")
datatable(sphist)

51.7 Simplify Graph - removing loops and duble edges

#full_no_zero_without_CAPS_u<-simplify(full_no_zero_without_CAPS) #Simplify

full_no_zero_without_CAPS_u<-as.undirected(full_no_zero_without_CAPS, mode="collapse",edge.attr.comb=list(weight="mean","ignore"))

52 Find cliques (complete subgraphs of an undirected graph)

Graph clique is a subset of vertices of a graph such that every two vertices in the clique are adjacent. - To check*

52.1 Number of cliques - subgraphs

cliques_full_no_zero_without_CAPS_u<-cliques(full_no_zero_without_CAPS_u) # list of cliques 
length(cliques_full_no_zero_without_CAPS_u)
## [1] 19904

52.2 Number of cliques by cliques size

cliques_full_no_zero_without_CAPS_u_size<-sapply(cliques(full_no_zero_without_CAPS_u), length) 
cliques_full_no_zero_without_CAPS_u_size_t<-table(cliques_full_no_zero_without_CAPS_u_size)
cliques_full_no_zero_without_CAPS_u_size_t
## cliques_full_no_zero_without_CAPS_u_size
##    1    2    3    4    5    6    7    8    9   10 
##  186 1129 2614 4188 4848 3933 2125  725  143   13

52.3 Cliques Bar Plot Sizes Frequency

barplot(cliques_full_no_zero_without_CAPS_u_size_t)
title(main = "Cliques Sizes Frequency - Bar Plot fuull_no_zero_fancy", font.main = 4)

52.4 Size of largest clique

A maximum clique is a clique that cannot be extended by including one more adjacent vertex (not included in larger one).

clique_num(full_no_zero_without_CAPS_u)
## [1] 10

52.5 Number of maximal cliques

count_max_cliques(full_no_zero_without_CAPS_u)
## [1] 660

52.6 Finding of largest cliques

largest_cliques<-largest_cliques(full_no_zero_without_CAPS_u) # cliques with max number of nodes
length(largest_cliques)
## [1] 13

52.7 Plotting the largest cliques - important to consider connectivite

#Coloring largest clique as gold and others one as gray
vcol <- rep("grey80", vcount(full_no_zero_without_CAPS_u))
vcol[unlist(largest_cliques(full_no_zero_without_CAPS_u))] <- "gold"

#Saving gray and gold as igraph attribute
V(full_no_zero_without_CAPS_u)$vcol<-vcol

#Saving labels to display as legend
V(full_no_zero_without_CAPS_u)$vcollabel[V(full_no_zero_without_CAPS_u)$vcol=="gold"]<-"Largets Clique"
V(full_no_zero_without_CAPS_u)$vcollabel [V(full_no_zero_without_CAPS_u)$vcol=="grey80"]<-"Others"

52.8 Plotting Clique Size

set.seed(123)
#Plotting based only on degree measures 
edge.start <- ends(full_no_zero_without_CAPS_u, es=E(full_no_zero_without_CAPS_u), names=F)[,1]

# Fixing ego
minC <- rep(-Inf, vcount(full_no_zero_without_CAPS_u))
maxC <- rep(Inf, vcount(full_no_zero_without_CAPS_u))
minC[1] <- maxC[1] <- 0
co <- layout_with_fr(full_no_zero_without_CAPS_u, niter=10^4, minx=minC, maxx=maxC,miny=minC, maxy=maxC, weights=E(full_no_zero_without_CAPS_u)$full_no_zero_without_CAPS)

#Plotting
plot(full_no_zero_without_CAPS_u, 
     layout=co,
     edge.color=V(full_no_zero_without_CAPS_u)$vcol[edge.start],
     #edge.arrow.size=E(full_no_zero_without_CAPS_u)$full_no_zero_without_CAPS/2000*mean(E(full_no_zero_without_CAPS_u)$full_no_zero_without_CAPS),
     #edge.width=E(full_no_zero_without_CAPS_u)$full_no_zero_without_CAPS/20*mean(E(full_no_zero_without_CAPS_u)$full_no_zero_without_CAPS),
     edge.curved = TRUE,
     vertex.color=vcol,
     vertex.size=log(degree(full_no_zero_without_CAPS_u)+2)*10,
     vertex.frame.color="black",
     vertex.label.color="black",
     vertex.label=get.vertex.attribute(full_no_zero_without_CAPS_u,"LABEL_COR"),
     vertex.label.cex=log(degree(full_no_zero_without_CAPS_u)+2)/10,
     vertex.label.dist=0,
     rescale=F,
     xlim=range(co[,1]), 
     ylim=range(co[,2])
     )
axis(1)
axis(2)

#Solving Problems with legend rendering 
a<-V(full_no_zero_without_CAPS_u)$vcollabel
b<-V(full_no_zero_without_CAPS_u)$vcol
c<-table(a,b)
d<-as.data.frame(c)
e<-subset(d, d$Freq>0)
f<-t(e$a)
g<-t(e$b)

#Adding Legend
legend(x=range(co[,1])[2], 
       y=range(co[,2])[2],
       legend=as.character(f),
       pch=21,
       col = "#777777", 
       pt.bg=as.character(g),
       pt.cex=2,
       bty="n", 
       ncol=1,
       lty=1,
       cex = .3)


#Adding Title
  title("Network Vertex Degree Sized - classfied by largest clique vs. others", sub = "Source: from authors ")  
  text( 
    x=range(co[,1])[1],
    y=range(co[,2])[1], 
      labels =       sprintf("Size of largest clique: %.1f\nNumber of maximal cliques: %.1f",
     clique_num(full_no_zero_without_CAPS_u), 
     count_max_cliques(full_no_zero_without_CAPS_u)
             )
       )

# Re-generate dataframes for both nodes and edges, now containing
# calculated network attributes
full_no_zero_without_CAPS_node_list <- get.data.frame(full_no_zero_without_CAPS, what = "vertices")

#Write Node List
write.csv(full_no_zero_without_CAPS_node_list, "~/SNArRDJF/Tese_Pedro/SemCAPS/Banco/full_no_zero_without_CAPS_node_list_df.csv")

# Determine a community for each edge. If two nodes belong to the
# same community, label the edge with that community. If not,
# the edge community value is 'NA'
full_no_zero_without_CAPS_edge_list <- get.data.frame(full_no_zero_without_CAPS, what = "edges")

#Write Node List
write.csv(full_no_zero_without_CAPS_edge_list, "~/SNArRDJF/Tese_Pedro/SemCAPS/Banco/full_no_zero_without_CAPS_edge_list.csv")

#Write Node List
write.csv(full_no_zero_without_CAPS_edge_list, "~/SNArRDJF/Tese_Pedro/SemCAPS/Banco/full_no_zero_without_CAPS_edge_list.csv")

53 Saving objects with new variables and changes

save.image("~/SNArRDJF/Robject/full_no_zero_without_CAPS_dataframe.RData")